The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

running a Slim application from a subdirectory

Bob Humphrey's Avatar

Bob Humphrey

01 Mar, 2012 09:21 PM

What changes would I need to make to run a Slim application from a subdirectory rather than the root web directory?

  1. 1 Posted by Bill on 02 Mar, 2012 02:09 PM

    Bill's Avatar

    You don't need to make any changes... But your views need to output paths with the sub directory in mind.

    One way to do this:

    $app_base = $app->request()->getRootUri()

    And push the $app_base to your views:

    $app->render('my_view.php', array('app_base' => $app_base));

    In your view:

    <img src="<?= $app_base ?>/static/img/logo.png" />

    If the above sounds like a hassle - you do this often - then you could place it in a plugin.

    $app->hook('slim.before.dispatch', function() use ($app) {
            'app_base' => $app->request()->getRootUri()

    Or you could set up a global constant:

    define('MY_APP_BASE', $app->request()->getRootUri() );

    <img src="<?= MY_APP_BASE ?>/static/img/logo.png" />

    But it's easy to get carried away with globals!

    You could alternatively use relative urls in your views, but it can get confusing, unless your urls follow the same structure.

    You could explicitly state all your urls from your root:

    <img src="/path/to/slim_app/static/img/logo.png" />

    Then if you move your app, you can do a find and replace across all your views.

    Or you could use an alias on your web server, for something like the static directory.

    Using named routes might also help you, as they appear to take into account the subdirectory, in the outputted urls. And it's quite likely you'll need to output app links within your views. See: $app->urlFor().

    Hope that helps.

  2. 2 Posted by Bob Humphrey on 03 Mar, 2012 07:52 PM

    Bob Humphrey's Avatar

    Thank you. This information was helpful for routes that respond to GET requests. I'm still having problems with the routes that handle POST requests. The application is just returning a 404 Page Not Found response. I'm trying to get the tutorial app from Nettuts running ( I've attached the index.php file.

  3. 3 Posted by Bill on 04 Mar, 2012 12:05 AM

    Bill's Avatar

    Existing code sample:

    // '/admin/edit/:id'
    return $app->render('admin_input.html', array(
            'action_name'   =>      'Edit', 
            'action_url'    =>      '/admin/edit/' . $id,
            'article'       =>      $article

    Your action_url is probably causing you the problem.

    If you leave it blank. So in the form you have:

    <form action="" method="post">

    It will post to itself. In your example your post url is the same as the get
    url, so this will work.

    Or you can use named routes which help here:

    $app->post('/admin/edit/(:id)', $authCheck, function($id) use ($app) {
        // code here

    So the above now has an alias that you can use to construct an url.

    Ammended code sample:

    // '/admin/edit/:id'
    return $app->render('admin_input.html', array(
            'action_name'   =>      'Edit', 
            'action_url'    =>      $app->urlFor('admin-edit-post', array('id'=>$id) ),
            'article'       =>      $article

    Hope that helps.

  4. 4 Posted by Bill on 04 Mar, 2012 12:08 AM

    Bill's Avatar



    Should be:


    Oh and you'll have to do something similar with the redirect after the post.

    What does everyone else do?

  5. Support Staff 5 Posted by Josh Lockhart on 24 Mar, 2012 08:01 PM

    Josh Lockhart's Avatar

    Slim should work automatically in a sub-directory (make sure you've got the necessary .htaccess file installed correctly). In your code that you attached, you do not need to return the result of $app->render(). Instead, you only need to invoke (not return) $app->render(). This method invocation will automatically echo the rendered template to the output buffer (later captured and appended to the response body).

    Let me know if you are still having issues.

  6. 6 Posted by Bill on 26 Mar, 2012 12:39 PM

    Bill's Avatar

    Oops! Didn't notice the explicit return there!

  7. Andrew Smith closed this discussion on 02 Aug, 2012 10:52 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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