HTTP Controllers

Introduction

Instead of defining all of your request handling logic in a single routes.php file, you may wish to organize this behavior using Controller classes. Controllers can group related HTTP request handling logic into a class. Controllers are typically stored in the app/Http/Controllers directory.

Basic Controllers

Here is an example of a basic controller class. All Lumen controllers should extend the base controller class included with the default Lumen installation:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\User;
  4. class UserController extends Controller
  5. {
  6. /**
  7. * Show the profile for the given user.
  8. *
  9. * @param int $id
  10. * @return Response
  11. */
  12. public function showProfile($id)
  13. {
  14. return view('user.profile', ['user' => User::findOrFail($id)]);
  15. }
  16. }

We can route to the controller action like so:

  1. $app->get('user/{id}', '[email protected]');

Now, when a request matches the specified route URI, the showProfile method on the UserController class will be executed. Of course, the route parameters will also be passed to the method.

Controllers & Namespaces

It is very important to note that we did not need to specify the full controller namespace when defining the controller route. We only defined the portion of the class name that comes after the App\Http\Controllers namespace "root". By default, the bootstrap/app.php file will load the routes.php file within a route group containing the root controller namespace.

If you choose to nest or organize your controllers using PHP namespaces deeper into the App\Http\Controllers directory, simply use the specific class name relative to the App\Http\Controllers root namespace. So, if your full controller class is App\Http\Controllers\Photos\AdminController, you would register a route like so:

  1. $app->get('foo', 'Photos\[email protected]');

Naming Controller Routes

Like Closure routes, you may specify names on controller routes:

  1. $app->get('foo', ['uses' => '[email protected]', 'as' => 'name']);

Once you have assigned a name to the controller route, you can easily generate URLs to the action. To generate a URL to a controller action, use the action helper method. Again, we only need to specify the part of the controller class name that comes after the base App\Http\Controllers namespace:

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

You may also use the route helper to generate a URL to a named controller route:

  1. $url = route('name');

Controller Middleware

Middleware may be assigned to the controller's routes like so:

  1. $app->get('profile', [
  2. 'middleware' => 'auth',
  3. 'uses' => '[email protected]'
  4. ]);

However, it is more convenient to specify middleware within your controller's constructor. Using the middleware method from your controller's constructor, you may easily assign middleware to the controller. You may even restrict the middleware to only certain methods on the controller class:

  1. class UserController extends Controller
  2. {
  3. /**
  4. * Instantiate a new UserController instance.
  5. *
  6. * @return void
  7. */
  8. public function __construct()
  9. {
  10. $this->middleware('auth');
  11. $this->middleware('log', ['only' => ['fooAction', 'barAction']]);
  12. $this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
  13. }
  14. }

Dependency Injection & Controllers

Constructor Injection

The Lumen service container is used to resolve all Lumen controllers. As a result, you are able to type-hint any dependencies your controller may need in its constructor. The dependencies will automatically be resolved and injected into the controller instance:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Repositories\UserRepository;
  4. class UserController extends Controller
  5. {
  6. /**
  7. * The user repository instance.
  8. */
  9. protected $users;
  10. /**
  11. * Create a new controller instance.
  12. *
  13. * @param UserRepository $users
  14. * @return void
  15. */
  16. public function __construct(UserRepository $users)
  17. {
  18. $this->users = $users;
  19. }
  20. }

Method Injection

In addition to constructor injection, you may also type-hint dependencies on your controller's action methods. For example, let's type-hint the Illuminate\Http\Request instance on one of our methods:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. class UserController extends Controller
  5. {
  6. /**
  7. * Store a new user.
  8. *
  9. * @param Request $request
  10. * @return Response
  11. */
  12. public function store(Request $request)
  13. {
  14. $name = $request->input('name');
  15. //
  16. }
  17. }

If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. class UserController extends Controller
  5. {
  6. /**
  7. * Update the specified user.
  8. *
  9. * @param Request $request
  10. * @param int $id
  11. * @return Response
  12. */
  13. public function update(Request $request, $id)
  14. {
  15. //
  16. }
  17. }