Agu Dondo
Agu Dondo

Reputation: 13569

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

Upvotes: 5

Views: 12446

Answers (1)

bonope
bonope

Reputation: 91

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

Upvotes: 3

Related Questions