Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 735
Agu Dondo Avatar asked Oct 21 '22 00:10

Agu Dondo


1 Answers

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

like image 142
bonope Avatar answered Oct 27 '22 10:10

bonope