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 do comments in Blade files?

How to output (echo/print) content in PHP

What is the difference between reference and primitive data types types in Javascript?

A list of operators on Eloquent's where() method

How to fix the MySQL related "Specified key was too long error" error in Laravel

How to force non www in htaccess

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

How to change the order that migrations happen?

How to set up custom error pages in .htaccess for your Apache server (ErrorDocument)

How to sort an array in javascript?