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

redirect not redirecting - stays on same page

Benjamin's Avatar

Benjamin

13 Jan, 2014 04:22 PM

Hey guys,

I've been using Slim for a while now and I love it. Locally, everything is working fine, but when I tried testing my project on our college server, redirects aren't working at all.

The problem lies in $app->redirect(). I've tried using it with an URL directly or via $app->urlFor and both fail silenty. They just don't redirect. The browser stays on the same page with no error or message whatsoever.

Code example:

$app->get("/about/", function() use ($app, $data) { $app->redirect($app->urlFor("aboutHistory")); });

$app->get("/about/history/", function() use ($app, $data) { $data["site"] = "about-history"; $data["title"] = "Über den Kurs | Historisches"; $data["subfile"] = "history"; $app->render("static/header.tpl", $data); $app->render("about.tpl", $data); $app->render("static/footer.tpl", $data); })->name("aboutHistory");

When I visit /about/ the browser stays on that page, completely blank, no error, although that could have been disabled by our server admins. I will consult with him on Thursday, but I wanted to get feedback on this problem here, maybe someone knows what's up. Locally with XAMPP, everything works fine.

Thank you.

  1. 1 Posted by uwe.d.wagner on 14 Jan, 2014 10:35 AM

    uwe.d.wagner's Avatar

    Something is wrong with your code snippet. There are three render methods inside one function:

    
    $app->render("static/header.tpl", $data); 
    $app->render("about.tpl", $data); 
    $app->render("static/footer.tpl", $data); })->name("aboutHistory");
    
  2. 2 Posted by Benjamin on 14 Jan, 2014 04:37 PM

    Benjamin's Avatar

    Yes, but how is that a problem? I render three different files.

  3. 3 Posted by uwe.d.wagner on 14 Jan, 2014 09:45 PM

    uwe.d.wagner's Avatar

    Yes I see you are right. The following code works as expected:

    $app->view->setData(array('site'    => 'about-history',
                                                  'title'   => 'Über den Kurs',
                                                  'subfile' => 'history'));
    
    
    $app->get("/about", function() use ($app) { 
        $app->redirect($app->urlFor("aboutHistory")); });
    
    $app->get("/about/history/", function() use ($app ) { 
        
        $app->render("header.php"); 
        $app->render("about.php"); 
        $app->render("footer.php"); })->name("aboutHistory");
        
    $app->run();
    
  4. 4 Posted by Benjamin on 15 Jan, 2014 12:36 PM

    Benjamin's Avatar

    After a bit of searching around, I think the problem might be related to output buffering. Locally, it's enabled, while it's disabled on the server. Would be great if someone with knowledge of the Slim code base could comment on this guess.

  5. 5 Posted by Andy on 25 Jul, 2014 01:54 PM

    Andy's Avatar

    Did you ever get this sorted? I'm seeing the same thing. Works great on production. Live environment it won't redirect? No errors, just stays on same page and page is blank?

  6. 6 Posted by Benjamin Kaspar on 25 Jul, 2014 02:04 PM

    Benjamin Kaspar's Avatar

    Hey Andy,

    in case I remember correctly, the problem really was caused by the output
    buffering. It was disabled for some reason on our production server, which
    caused the redirect function to fail. So see if OB is disabled for you and
    try to enable it and see if it works.

  7. 7 Posted by Andy on 25 Jul, 2014 02:28 PM

    Andy's Avatar

    Thanks for the speedy response. I'll investigate. If I place a PHP header redirect at the top of index.php it redirects, however nothing is seemingly working inside the routes.

  8. 8 Posted by Andy on 25 Jul, 2014 03:21 PM

    Andy's Avatar

    Amazing. That was it! Thanks so much.

    I've not really used output buffering before. I run a few sites on this server - it won't impact anything else simply by turning on, would it?

  9. 9 Posted by Benjamin Kaspar on 25 Jul, 2014 05:50 PM

    Benjamin Kaspar's Avatar

    Glad it works. I never heard of any problems with OB turned on and
    personally never encountered any problems.

  10. 10 Posted by asAD on 18 May, 2019 07:09 AM

    asAD's Avatar
    
        require 'Slim/Slim.php';
    
        $app = new \Slim\Slim();
    
        //GET route
        $app->get('/hello/:name', function ($name) {
            echo "Hello, $name";
        });
    
        //POST route
        $app->post('/person', function () {
            //Create new Person
        });
    
        //PUT route
        $app->put('/person/:id', function ($id) {
            //Update Person identified by $id
        });
    
        //DELETE route
        $app->delete('/person/:id', function ($id) {
            //Delete Person identified by $id
        });     
    
    
        $app->run();
    ?>```
    

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