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 select all rows for a certain month (or day, year or time), using Eloquent

301 Redirect a single URL in .htaccess to a new location

How to round a number in JS?

How to add the CSRF (Cross-site request forgery) token in Laravel?

How to select what columns to return when calling ::all()

How to hide the server signature (hide Apache info) from HTTP headers with .htaccess

How to do comments in Blade files?

How to block an IP address in .htaccess

PHP Date commonly used formats

How to merge/concat two arrays together in JS?