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

Laravel: 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. });
webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to make multiple routes share the same URL structure or URL parameters

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to create random numbers in JS?

An overview of every main PHP array function

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

What is the loop variable in a @foreach loop in Blade?

How to merge/concat two arrays together in JS?

How to find out which php.ini file PHP is using?

How to set the Expires header in .htaccess

How to prevent Eloquent from adding created_at or updated_at timestamps?

How to group a SQL statement in brackets/parenthesis

How to block an IP address in .htaccess