The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

rendering approach for status updates

boschen's Avatar

boschen

09 Feb, 2016 04:54 PM

Hello, I searched to forum for related questions but did not find a good match, I hope this isn't a repeat.

I am using Slim V2 with Twig and trying to echo a series of progress messages within a viewport. I would like each message to appear after the completion of each process with the previous messages still view-able (as opposed to a flash message).

I took the approach of saving $console_message as a session variable, and appending it with each new message and then rendering the result basically as follows:

//==========================================================
$app -> get('/import/:step', function ($step) use($app) {

    //load prior messages
    $console_message=$_SESSION['console_message'];
    switch ($step) {

      case 'step1':
      // do process for step 1 here
      $console_message.="Step 1 done. \n";
      $_SESSION['console_message']= $console_message;
      $app ->render("/site/admin.twig", array('console_message'=> $console_message));
      $app ->redirect("./step2");

      case 'step2':
      // do process for step 2 here
      $console_message.="Step 2 done. \n";
      $_SESSION['console_message']= $console_message;
      $app ->render("/site/admin.twig", array('console_message'=> $console_message));
      $app ->redirect("./step3");

      case 'step3':
       //etc...
       }
}
$app->run
//==========================================================

The above will render the messages once the entire process completes. I do see that the redirects make my prior renders useless as it is the final one that gets applied. I am looking for the best approach to have the page update with the latest message after each step and not really sure how to go about it using Slim and Twig. I guess I could put a redirect after the final $app->run and store the state of the last update in a session variable- is this the best approach (will that even work?) or is there a better way?

  1. 1 Posted by boschen on 09 Feb, 2016 08:38 PM

    boschen's Avatar

    As an update- I changes the above by storing the step in a session variable and not passing the parameter in the URL and putting the redirect after the $app->run command:

    //initiate step variable from prior operation:
     if (!isset($_SESSION['import_step'])) {
             $step=1;
     } else {
            $step = $_SESSION['import_step'];
     }

    $app->get('/import', function() use ($app) {
        $console_message=$_SESSION['console_message'];
        $step = $_SESSION['import_step'];

        switch($step) {
               case 1:
                        $console_message.="Step 1 done. \n";
                        $_SESSION['console_message']= $console_message;
                        $app ->render("/site/admin.twig", array('console_message'=> $console_message));
               case 2:
                        $console_message.="Step 2 done. \n";
                        $_SESSION['console_message']= $console_message;
                        $app ->render("/site/admin.twig", array('console_message'=> $console_message));
              case 3:
                        //etc...
              }
         }
    $app ->run();

    if (isset($_SESSION('import_step')])) {
        //once step is initiated the process will start until step reaches 4
         $step = $_SESSION['import_step'];
        if ($step<4) {
            //process continues until step 4
            $app -> redirect("./import");
           }
    }

    Although not confident the above approach is best, I still would have thought it would work. However the pages are rendered below each other for each step called rather than replacing the previous page rendered. I thought that by placing the redirect after the $app->run any new route with a render would replace not add on to the previous render. I added this update as progress in case it assists in developing a real solution.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac