We help IT Professionals succeed at work.

Laravel - Routing


For the past number of years my projects have been based on my own PHP framework, it's simple, but has worked.

I've started to look into other framework and Laravel seems to come top of mosts lists I look at. I've started looking at the possibility of using it in future projects, but there seems to be something I don't know if it can do.

Firstly, my frame has a folder structure like this;

      - routes
          - admin
               - controller
               - view
               - www
          - api
               - controller
          - public
               - controller
               - view
               - www

Urls such as www.domain.com/public/ are mapped to /app/routes/public/, and www.domain.com/admin/ are mapped to /app/routes/admin/. I like doing is this way as it keeps admin interface files away from public facing files, it's much more organised in my opinion.

Secondly, in my framework I don't need to define each route as you do in Laravel. /admin/users/list_active/ loads the list_active() method that's in the users controller inside the admin route folder.

Most of the projects I work on involve an admin interface, and an API. Can I do the above in Laravel without having all of the controllers in one place. I really don't like the idea of having to prefix controller names with api-userController, or admin-userController, it just feels messy and un-organised.

Is there a way to have Laravel catch all routes and automatically route them to the correct controller/method? In hindsight I don't think I might too much having to define routes, as I can see the benefits in doing this for API's i.e. /user/69/messages/h76dg/ being mapped to user::messages($user_id,$message_id);

Any help would be appreciated.
Watch Question

Most Valuable Expert 2011
Top Expert 2016
My experience is with Laravel 4.2 (5.0 seems to be current / recent).  If you choose "resourceful" routing, Laravel will assume that you are using the default set of named routes.  However if you forgo the default and name your routes explicitly, Laravel will use introspection* to examine its own code and your routes, and will set up the routing correctly.

* In PHP I think this might be called Reflection.

Suggest you experiment with this a little bit - I had to at first but after a few different attempts it made a lot of sense.