Controllers

Basic Controllers

Instead of defining all of your route-level logic in a single routes.php file, you may wish to organize this behavior using Controller classes. Controllers can group related route logic into a class, as well as take advantage of more advanced framework features such as automatic dependency injection.

Controllers are typically stored in the app/controllers directory, and this directory is registered in the classmap option of your composer.json file by default. However, controllers can technically live in any directory or any sub-directory. Route declarations are not dependent on the location of the controller class file on disk. So, as long as Composer knows how to autoload the controller class, it may be placed anywhere you wish.

Here is an example of a basic controller class:

  1. class UserController extends BaseController {
  2. /**
  3. * Show the profile for the given user.
  4. */
  5. public function showProfile($id)
  6. {
  7. $user = User::find($id);
  8. return View::make('user.profile', array('user' => $user));
  9. }
  10. }

All controllers should extend the BaseController class. The BaseController is also stored in the app/controllers directory, and may be used as a place to put shared controller logic. The BaseController extends the framework's Controller class. Now, we can route to this controller action like so:

  1. Route::get('user/{id}', '[email protected]');

If you choose to nest or organize your controller using PHP namespaces, simply use the fully qualified class name when defining the route:

  1. Route::get('foo', 'Namespace\[email protected]');

Note: Since we're using Composer to auto-load our PHP classes, controllers may live anywhere on the file system, as long as composer knows how to load them. The controller directory does not enforce any folder structure for your application. Routing to controllers is entirely de-coupled from the file system.

You may also specify names on controller routes:

  1. Route::get('foo', array('uses' => '[email protected]', 'as' => 'name'));

To generate a URL to a controller action, you may use the URL::action method or the action helper method:

  1. $url = URL::action('[email protected]');
  2. $url = action('[email protected]');

You may access the name of the controller action being run using the currentRouteAction method:

  1. $action = Route::currentRouteAction();

Controller Filters

Filters may be specified on controller routes similar to "regular" routes:

  1. Route::get('profile', array('before' => 'auth',
  2. 'uses' => '[email protected]'));

However, you may also specify filters from within your controller:

  1. class UserController extends BaseController {
  2. /**
  3. * Instantiate a new UserController instance.
  4. */
  5. public function __construct()
  6. {
  7. $this->beforeFilter('auth', array('except' => 'getLogin'));
  8. $this->beforeFilter('csrf', array('on' => 'post'));
  9. $this->afterFilter('log', array('only' =>
  10. array('fooAction', 'barAction')));
  11. }
  12. }

You may also specify controller filters inline using a Closure:

  1. class UserController extends BaseController {
  2. /**
  3. * Instantiate a new UserController instance.
  4. */
  5. public function __construct()
  6. {
  7. $this->beforeFilter(function()
  8. {
  9. //
  10. });
  11. }
  12. }

If you would like to use another method on the controller as a filter, you may use @ syntax to define the filter:

  1. class UserController extends BaseController {
  2. /**
  3. * Instantiate a new UserController instance.
  4. */
  5. public function __construct()
  6. {
  7. $this->beforeFilter('@filterRequests');
  8. }
  9. /**
  10. * Filter the incoming requests.
  11. */
  12. public function filterRequests($route, $request)
  13. {
  14. //
  15. }
  16. }

Implicit Controllers

Laravel allows you to easily define a single route to handle every action in a controller. First, define the route using the Route::controller method:

  1. Route::controller('users', 'UserController');

The controller method accepts two arguments. The first is the base URI the controller handles, while the second is the class name of the controller. Next, just add methods to your controller, prefixed with the HTTP verb they respond to:

  1. class UserController extends BaseController {
  2. public function getIndex()
  3. {
  4. //
  5. }
  6. public function postProfile()
  7. {
  8. //
  9. }
  10. public function anyLogin()
  11. {
  12. //
  13. }
  14. }

The index methods will respond to the root URI handled by the controller, which, in this case, is users.

If your controller action contains multiple words, you may access the action using "dash" syntax in the URI. For example, the following controller action on our UserController would respond to the users/admin-profile URI:

  1. public function getAdminProfile() {}

RESTful Resource Controllers

Resource controllers make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages "photos" stored by your application. Using the controller:make command via the Artisan CLI and the Route::resource method, we can quickly create such a controller.

To create the controller via the command line, execute the following command:

  1. php artisan controller:make PhotoController

Now we can register a resourceful route to the controller:

  1. Route::resource('photo', 'PhotoController');

This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.

Actions Handled By Resource Controller

VerbPathActionRoute Name
GET/resourceindexresource.index
GET/resource/createcreateresource.create
POST/resourcestoreresource.store
GET/resource/{resource}showresource.show
GET/resource/{resource}/editeditresource.edit
PUT/PATCH/resource/{resource}updateresource.update
DELETE/resource/{resource}destroyresource.destroy

Sometimes you may only need to handle a subset of the resource actions:

  1. php artisan controller:make PhotoController --only=index,show
  2. php artisan controller:make PhotoController --except=index

And, you may also specify a subset of actions to handle on the route:

  1. Route::resource('photo', 'PhotoController',
  2. array('only' => array('index', 'show')));
  3. Route::resource('photo', 'PhotoController',
  4. array('except' => array('create', 'store', 'update', 'destroy')));

By default, all resource controller actions have a route name; however, you can override these names by passing a names array with your options:

  1. Route::resource('photo', 'PhotoController',
  2. array('names' => array('create' => 'photo.build')));

Handling Nested Resource Controllers

To "nest" resource controllers, use "dot" notation in your route declaration:

  1. Route::resource('photos.comments', 'PhotoCommentController');

This route will register a "nested" resource that may be accessed with URLs like the following: photos/{photoResource}/comments/{commentResource}.

  1. class PhotoCommentController extends BaseController {
  2. public function show($photoId, $commentId)
  3. {
  4. //
  5. }
  6. }

Adding Additional Routes To Resource Controllers

If it becomes necessary for you to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:

  1. Route::get('photos/popular', '[email protected]');
  2. Route::resource('photos', 'PhotoController');

Handling Missing Methods

When using Route::controller, a catch-all method may be defined which will be called when no other matching method is found on a given controller. The method should be named missingMethod, and receives the method and parameter array for the request:

Defining A Catch-All Method

  1. public function missingMethod($parameters = array())
  2. {
  3. //
  4. }

If you are using resource controllers, you should define a __call magic method on the controller to handle any missing methods.