HTTP Routing

Basic Routing

You will define all of the routes for your application in the routes/web.php file. The most basic Lumen routes simply accept a URI and a Closure:

  1. $router->get('foo', function () {
  2. return 'Hello World';
  3. });
  4. $router->post('foo', function () {
  5. //
  6. });

Available Router Methods

The router allows you to register routes that respond to any HTTP verb:

  1. $router->get($uri, $callback);
  2. $router->post($uri, $callback);
  3. $router->put($uri, $callback);
  4. $router->patch($uri, $callback);
  5. $router->delete($uri, $callback);
  6. $router->options($uri, $callback);

Route Parameters

Required Parameters

Of course, sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user’s ID from the URL. You may do so by defining route parameters:

  1. $router->get('user/{id}', function ($id) {
  2. return 'User '.$id;
  3. });

You may define as many route parameters as required by your route:

  1. $router->get('posts/{postId}/comments/{commentId}', function ($postId, $commentId) {
  2. //
  3. });

Route parameters are always encased within “curly” braces. The parameters will be passed into your route’s Closure when the route is executed.

Note: Route parameters cannot contain the - character. Use an underscore (_) instead.

Optional Parameters

You may define optional route parameters by enclosing part of the route URI definition in [...]. So, for example, /foo[bar] will match both /foo and /foobar. Optional parameters are only supported in a trailing position of the URI. In other words, you may not place an optional parameter in the middle of a route definition:

  1. $router->get('user[/{name}]', function ($name = null) {
  2. return $name;
  3. });

Regular Expression Constraints

You may constrain the format of your route parameters by defining a regular expression in your route definition:

  1. $router->get('user/{name:[A-Za-z]+}', function ($name) {
  2. //
  3. });

Named Routes

Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route using the as array key when defining the route:

  1. $router->get('profile', ['as' => 'profile', function () {
  2. //
  3. }]);

You may also specify route names for controller actions:

  1. $router->get('profile', [
  2. 'as' => 'profile', 'uses' => '[email protected]'
  3. ]);

Generating URLs To Named Routes

Once you have assigned a name to a given route, you may use the route’s name when generating URLs or redirects via the global route function:

  1. // Generating URLs...
  2. $url = route('profile');
  3. // Generating Redirects...
  4. return redirect()->route('profile');

If the named route defines parameters, you may pass the parameters as the second argument to the route function. The given parameters will automatically be inserted into the URL in their correct positions:

  1. $router->get('user/{id}/profile', ['as' => 'profile', function ($id) {
  2. //
  3. }]);
  4. $url = route('profile', ['id' => 1]);

Route Groups

Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route. Shared attributes are specified in an array format as the first parameter to the $router->group method.

To learn more about route groups, we’ll walk through several common use-cases for the feature.

Middleware

To assign middleware to all routes within a group, you may use the middleware key in the group attribute array. Middleware will be executed in the order you define this array:

  1. $router->group(['middleware' => 'auth'], function () use ($router) {
  2. $router->get('/', function () {
  3. // Uses Auth Middleware
  4. });
  5. $router->get('user/profile', function () {
  6. // Uses Auth Middleware
  7. });
  8. });

Namespaces

Another common use-case for route groups is assigning the same PHP namespace to a group of controllers. You may use the namespace parameter in your group attribute array to specify the namespace for all controllers within the group:

  1. $router->group(['namespace' => 'Admin'], function() use ($router)
  2. {
  3. // Using The "App\Http\Controllers\Admin" Namespace...
  4. $router->group(['namespace' => 'User'], function() use ($router) {
  5. // Using The "App\Http\Controllers\Admin\User" Namespace...
  6. });
  7. });

Route Prefixes

The prefix group attribute may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin:

  1. $router->group(['prefix' => 'admin'], function () use ($router) {
  2. $router->get('users', function () {
  3. // Matches The "/admin/users" URL
  4. });
  5. });

You may also use the prefix parameter to specify common parameters for your grouped routes:

  1. $router->group(['prefix' => 'accounts/{accountId}'], function () use ($router) {
  2. $router->get('detail', function ($accountId) {
  3. // Matches The "/accounts/{accountId}/detail" URL
  4. });
  5. });