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

What does PEAR stand for?

How to get the type of a variable in JS?

How to block IPv6 IP addresses in .htaccess

How to reverse htmlentities() / What is the opposite of htmlentities()?

How to replace values in a string, in JS

How to hide directory listing in .htaccess?

An overview of every main PHP array function

What is the difference between empty(), isset(), != null, is_null() in PHP

How to set infinite execution time in a PHP script

How to Extract Query String (from a URL) Into an Associative Array in PHP