Route Groups

Route Prefix

Suppose we need certain routes to have common prefix, for example a admin has to manage posts, categories, users, etc, one way we can do it like this.  

<?php
Route::get('admin/posts', function(){ //some code });
Route::get('admin/categories', function(){ //some code });
Route::get('admin/users', function(){ //some code });

 

This works but see, our routes have admin prefix in common and if we add any more suffix or prefixes our routes will become mess to read, For the sake of readability we need some kind of namespacing, that's where route prefixes come in handy, let's see how to use it.

<?php
Route::group( array('prefix'=>'admin'), function(){

	Route::get('posts', function(){
		return view('admin.post');
	});

	Route::get('categories',function(){
		return view('admin.category');
	});

	Route::get('users',function(){
		return view('admin.user');
	});
});

 

Domain and Sub-Domain Routing

You should just change prefix name from application URI to your website domain.

<?php

Route::group( array('domain'=>'devartisans.com'), function(){
	route::get('users',function(){
		return view('admin.user');
	});
});

 

Now our full url name look like devartisans.com/users

 

Sub-Domain routing

 

<?php

Route::group( array('domain'=>'{user},devartisans.com'), function(){
	route::get('user/{id}',function($user, $id){
		//logic and redirect
	});
}); 

 

In the above code user is changeable and according to user Id also will change.

Something to say? Tell us in comment section.