Laravel Routes and Routing Cheatsheet - everything you need to know

Laravel Routes and Routing Cheatsheet

What are route patterns in Laravel?

In your routes file you can add code like this:

  1.     Route::pattern('id', '[0-9]+');

Then you can use that pattern later on in your routes file like this:

  1. Route::get('user/{id}', function ($id) {
  2.     // Only executed if {id} is numeric...
  3. });

I've not seen this used too often, however this is a useful feature and keeps things quite clean.

Some other examples of defining some route patterns:

  1. Route::pattern('slug', '[a-z0-9-]+');
  2. Route::pattern('hash', '[a-z0-9]+');
  3.  
  4. Route::get('forum-thread/{slug}', '[email protected]'); // {slug} has to be alpha numeric (lowercase), but can include a dash
  5. Route::get('forum-post/{hash}', '[email protected]'); // {hash} has to be alpha numeric, lowercase
View More Details (and 8 discussions about this topic) Here...

How to namespace a Laravel route group?

You can add a namespace for all items within a route group. If your controller (or controllers) are in app/Http/Controllers/AdminPanel then you could use the following code in your routes file (web.php):

  1. Route::group(["namespace"=>"AdminPanel"],function() {
  2.     Route::get("/","[email protected]"); // actually calls \App\Http\Controllers\AdminPanel\AdminControllers because of the namespace
  3. });

You can use relative (relative to the normal controller namespace - \App\Http\Controllers) or absolute (\App\Admin\Controllers\)

View More Details (and 11 discussions about this topic) Here...

How to use subdomains as parameters in your routes (or in your web.php file)

You can use subdomains in your routes. You can either have certain routes for certain subdomains, or pass the subdomain as a parameter in the routing.

  1. Route::domain('{account}.example.com')->group(function () {
  2.     Route::get('user/{id}', function ($account, $id) {
  3.         //
  4.     });
  5. });
View More Details (and 6 discussions about this topic) Here...

How to add a name prefix to all routes in a group?

You can use Route Name Prefixes. If you wanted to have a group of Laravel routes all with names like 'admin.users', 'admin.news', etc then you can use a route name prefix to prepend 'admin.' to the start of all of the names

  1. Route::name('admin.')->group(function () {
  2.     Route::get('users', function () {
  3.          })->name('users'); // Route will have the name "admin.users"...
  4.  
  5. });

However I do not like doing this, as it means I can't easily search for a particular route. But it does have the advantage of enforcing all in a group to have the same naming structure.

View More Details (and 5 discussions about this topic) Here...

How to access the Laravel's route parameters outside of the controller?

If you want to read the route params (that are normally passed to the controller) from somewhere else, you can use Route::current()->parameters().

For example:

  1. $request_params = Route::current()->parameters();
View More Details (and 6 discussions about this topic) Here...

How to make multiple routes share the same URL structure or URL parameters

If you have the following URLs:

/control-panel/{team_id}/
/control-panel/{team_id}/schedule
/control-panel/{team_id}/delete
        etc...

(They all share the first part of the url - /control-panel/{team_id})

Then you don't have to write out the routes like this:

  1. Route::get("control-panel/{team_id}","[email protected]");
  2. Route::get("control-panel/{team_id}/schedule","[email protected]");
  3. Route::delete("control-panel/{team_id}/delete","[email protected]");

Instead you can group the routes and add a prefix:

  1. Route::group(["prefix"=>"control-panel/{team_id}"], function() {
  2.     Route::get("/","[email protected]");
  3.     Route::get("schedule","[email protected]");
  4.     Route::delete("delete","[email protected]");
  5. });

Putting a group within a group

You can also put a Route::group() inside another one.

  1. Route::group(["prefix"=>"control-panel/{team_id}"], function() {
  2.     Route::get("/","[email protected]");
  3.  
  4.     Route::group(["prefix"=>"schedule"],function() {
  5.         Route::get("/","[email protected]");
  6.         Route::post("/add_new","[email protected]");
  7.     }
  8.     Route::delete("delete","[email protected]");
  9. });
View More Details (and 6 discussions about this topic) Here...