PHP - Slim Framework: Best practice with a lot of code inside routes closures
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
Agu Dondo
Updated on June 04, 2022Comments
-
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 over 10 yearsGood aproach, it's starting to make sense! Thanks a lot.
-
Bagusflyer about 10 yearsWhat is Class controller? Any example?
-
Werner over 9 yearsThe link @bonope provided has a clear example of a "Class COntroller".