laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.36k stars 10.97k forks source link

Routes defined in Controllers #298

Closed kalebheitzman closed 11 years ago

kalebheitzman commented 11 years ago

Love L4! This is an unconventional request/question. Is it possible to define routes inside of Controllers themselves instead of the routes.php file? Or maybe there is an answer to this: Is it possible to use a modular structure where you would have for example an Auth module/folder in the app directory. Within the auth folder there would be a config, controllers, models, views folder to add all of the different components. I'm just trying to figure out how to make my code more modular/shareable. I don't think composer is the answer to my question because it would be L4 specific.

andrewryno commented 11 years ago

Look into developing a package to handle all of that. You can define routes in a package, too.

See: http://four.laravel.com/docs/packages

bencorlett commented 11 years ago

@kalebheitzman, short andwer, no. Routes are explicit in L4, Symfony, etc, whereas they're implicit in CI, FuelPHP, Kohana etc.

Having explicit routes gives people a hell of a lot more control over their applications. The way L4's routing is architectured is that, "controllers" are actually converted into a Closure which responds to a route.

The current flow for controllers is:

  1. Define a route which has a controller as an action (e.g. GET /foo == FooController@getIndex).
  2. The creates a callback which extracts the data from your route and registers a standalone route which is indeed that callback.

OR

  1. Create a route with Route::controller, which gets an Inspector object to look in the controller class for routable methods (e.g., method prefixed with 'get', 'set' etc). It then comes back and starts from Number 1 in the above list.

tl;dr,

Routing is explicit. In @cartalyst's Platform 2.0, we have implicit routing with explicit overrides. This means, we have "extensions" which you can drop into your app, and they are automatically routed for convenience. For example, GET /users/groups may me automatically routed to a controller called Users\Controllers\GroupsController@index. But this is application specific and may vary for your own application.

kalebheitzman commented 11 years ago

Brilliant! Love it. This was the only thing holding me back as far as switching from CI. Thanks for the help.