{"id":8996,"date":"2023-11-21T09:01:39","date_gmt":"2023-11-21T09:01:39","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8996"},"modified":"2023-11-21T09:01:39","modified_gmt":"2023-11-21T09:01:39","slug":"laravel-route-group-parameters","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/laravel\/laravel-route-group-parameters","title":{"rendered":"Laravel: Route Group Parameter"},"content":{"rendered":"<p>All the laravel routes are defined in the <strong>Route<\/strong> directory. These files are automatically loaded by your application&#8217;s <strong>App\\Providers\\RouteServiceProvider<\/strong>.<\/p>\n<h3>Route Groups<\/h3>\n<p>Route groups allow you to share route attributes, such as middleware, across a large number of routes without needing to define those attributes on each individual route. We can use nested route groups also for the specific middleware, namespace, prefix, etc. Let&#8217;s see each one in detail:<\/p>\n<p><strong>1. Middleware<\/strong><br \/>\nTo assign middleware to all routes within a group, you may use the middleware key in the group attribute array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Route::group(['middleware' =&gt; 'auth'], function () {\r\n    Route::get('\/', function ()    {\r\n        \/\/ Uses Auth Middleware\r\n    });\r\n});\r\n<\/pre>\n<p><strong>2. Namespaces<\/strong><br \/>\nTo assign the same PHP namespace to a group of controllers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Route::group(['namespace' =&gt; 'Admin'], function()\r\n{\r\n    Route::group(['namespace' =&gt; 'User'], function()\r\n    { \r\n        \u2026\r\n    });\r\n});<\/pre>\n<p><strong>3. Route Prefixes<\/strong><br \/>\nThe prefix group array attribute may be used to prefix each route in the group with a given URI.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Route::group(['prefix' =&gt; 'admin'], function () {\r\n    Route::get('users', function ()    {\r\n        \/\/ Matches The \"\/admin\/users\" URL\r\n    });\r\n});<\/pre>\n<p><strong>4. Sub-Domain Routing<\/strong><br \/>\nRoute groups may also be used to route wildcard subdomains. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Route::group(['domain' =&gt; ['admin.example.com']], function()\r\n{\r\n    Route::get('users', function ()    {\r\n       \u2026\r\n    });\r\n});<\/pre>\n<p><strong>5. Controllers<\/strong><br \/>\nIf a group of routes all utilize the same controller, you may use the controller method to define the common controller for all of the routes within the group.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">use App\\Http\\Controllers\\OrderController;\r\n \r\nRoute::controller(OrderController::class)-&gt;group(function () {\r\n    Route::get('\/orders\/{id}', 'show');\r\n    Route::post('\/orders', 'store');\r\n});<\/pre>\n<p><strong>6. Route Name Prefixes<\/strong><br \/>\nThe name method may be used to prefix each route name in the group with a given string. The given string is prefixed to the route name exactly as it is specified, so we will be sure to provide the trailing . character in the prefix.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Route::name('admin.')-&gt;group(function () {\r\n    Route::get('\/users', function () {\r\n        \/\/ Route assigned name \"admin.users\"...\r\n    })-&gt;name('users');\r\n});<\/pre>\n<h3>Grouping Multiple Domains<\/h3>\n<p><strong>1. To group multiple domains, we can use pattern and group parameters to achieve this.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Route::pattern('domain', '(web.com|x.web.com)');\r\n\r\nRoute::group(['domain' =&gt; '{domain}'], function () {\r\n    Route::get('\/', function($domain) {\r\n        \/\/ Now all routes receive $domain as the first parameter\r\n    });\r\n});\r\n\r\n<\/pre>\n<p><strong>2. Another way to achieve this is to just declare the routes in a separate function as pass it to both route groups.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Route::group(['domain' =&gt; 'admin.example.com'], function()\r\n{\r\n    ...\r\n});\r\n\r\n$appRoutes = function() {\r\n    Route::get('\/',function(){\r\n        ...\r\n    }); \r\n};\r\n\r\nRoute::group(['domain' =&gt; 'app.example.com'], $appRoutes);\r\nRoute::group(['domain' =&gt; 'dev.app.example.com'], $appRoutes);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All the laravel routes are defined in the Route directory. These files are automatically loaded by your application&#8217;s App\\Providers\\RouteServiceProvider. Route Groups Route groups allow you to share route attributes, such as middleware, across a large number of routes without needing to define those attributes on each individual route. We can use nested route groups also [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8997,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[10],"tags":[],"class_list":["post-8996","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8996"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8996"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8996\/revisions"}],"predecessor-version":[{"id":9002,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8996\/revisions\/9002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8997"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}