The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Organizing larger apps with Slim

pan069's Avatar

pan069

14 Jul, 2012 01:22 AM

Hi,

New to Slim here. Just working my way through the docs. Slim seems to be very straight forward, I like that a lot. One on the things I seem to be missing from the docs is how to actually organize an application with Slim. If I may use some MVC terms to explain my question;

Say I have an application of say 10 controllers, or 10 different aspects, e.g. /users, /posts, /comments, /auth, etc. I'm supposed to chuck the code for all this in one big index.php? I probably just misunderstand but I was expecting something like Controllers to exist within Slim simply for the sake of organizing application logic in logical units. E.g. I was expecting to write something like this;

`$app->route('/users', UsersController);

class UsersController extends Controller {

public function get() {

  // Handle GET

} }`

But this doesn't seem to be the case. How do you guys organize your applications with Slim?

  1. Support Staff 1 Posted by Josh Lockhart on 14 Jul, 2012 01:24 AM

    Josh Lockhart's Avatar

    Keep in mind that slim is a micro-framework, so it's not going to have the architecture abilities from, say, CodeIgniter. However, I've written a blog post that you may want to read about this:

    http://www.slimframework.com/read/how-to-organize-a-large-slim-fram...

    Best,
    Josh

  2. 2 Posted by pan069 on 14 Jul, 2012 07:38 AM

    pan069's Avatar

    Thanks for that! Much appreciated.

    I was reading your post and it makes perfect sense but the following sort of got me asking questions:

    "a lightweight and nimble PHP framework used to build smaller web applications and APIs"

    When is a web application considered "smaller"? Can it be defined by the number of routes? Is it 7? 18?

    I think that no matter what size your application is, you will have to deal with organisational issues sooner or later. Your solution to the problem is pretty straight forward and within the context of Slim pretty much the only way. However, I think that Slim would easily be suitable for consideration of "larger" applications (what ever that might be) if it would help with the burden of organizing functionality into logical units (i.e. controllers).

    E.g. I'm currently considering Slim to build an API to front the business tier of an application, something for which it is very well suited. However, it would have been so great if the framework was helping me with the organisation of my logic.

    Ah well. Can't have everything. Keep up the good work!

    Cheers,
    Luke

  3. 3 Posted by Shimon on 15 Jul, 2012 05:25 AM

    Shimon's Avatar

    Luke,

    I think that you can find PSR-0 useful in your case.
    What you will do (which is the way I did it for my projects), is simply define routes and their classes & methods in the index.php, and keep the rest organized as per PSR-0 standard. Then you would also need to define your own autoloader and you're done.

    $app->route('/users', array('UsersController', 'getAll'));
    $app->route('/users/:id', array('UsersController', 'getById'));
    $app->run();
    
  4. 4 Posted by pan069 on 19 Jul, 2012 01:53 AM

    pan069's Avatar

    Thanks Shimon. That seems like a great compromise.

  5. Andrew Smith closed this discussion on 08 Aug, 2012 09:41 AM.

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

Keyboard shortcuts

Generic

? 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