PHP - Slim Framework: Best practice with a lot of code inside routes closures

12,205

Brian Nesbitt made a nice post about this: http://nesbot.com/2012/11/5/lazy-loading-slim-controllers-using-pimple.

If you don't want to use pimple, than you can get some idea from the section "Common first attempt", on how to separate you files.

update: Since version 2.4.0 you can use the inbuilt "Class controller": Version 2.4.0

Share:
12,205
Agu Dondo
Author by

Agu Dondo

Updated on June 04, 2022

Comments

  • Agu Dondo
    Agu Dondo almost 2 years

    I'm using Slim. In the documentation they only show examples working with only one index.php file, which has really little functionality for every route. For example:

    $app = new \Slim\Slim();
    $app->get('/books/:id', function ($id) {
        //Show book identified by $id
    });
    

    But in my case, my index.php file is getting bigger and bigger, now I have a lot of code for most routes, what is the best practice in this case? to include files inside the routes closures? What happens with the scope of global variables, like DB connection or app config? Thank you

  • Agu Dondo
    Agu Dondo over 10 years
    Good aproach, it's starting to make sense! Thanks a lot.
  • Bagusflyer
    Bagusflyer about 10 years
    What is Class controller? Any example?
  • Werner
    Werner over 9 years
    The link @bonope provided has a clear example of a "Class COntroller".