Updated May 18, 2023
Introduction to Laravel Route
The Laravel Route group allows the developer to route all the groups. This helps create clean codes, and whoever takes over the development of the website will be able to follow the exact routine. The other benefit of grouping the routes is that all the attributes can be shared with all the groups. This saves a lot of time. In any other framework, this should be done individually. However, with Laravel Framework, this feature appears as a force multiplier. Route grouping also saves duplication, which otherwise would have been the case if done individually. Attributes like middleware and namespaces can be shared in a group without doing it individually.
Syntax
The query looks like this:
Route::group( [ ] , callback);
Explanation: The Laravel framework is one of the most sought-after frameworks. It is expressive, and its library allows the developer to choose from many queries. This assists in creating robust functionalities. The laravel framework is also an immensely scalable framework capable of handling most challenges. One of the other features of the laravel framework is its flexible query command line system. This allows it to integrate a third-party command line. For eCommerce owners, it is a reason for employing laravel frameworks for their online business.
Use of Parameters
below are some parameters:
Code:
Route::get('/page/{number}', function ($number) {
echo "Your are on page ". $number;
});
Use of parameters that are optional and with values that are default:
Code:
Route::get('/page/{number?}', function ($number = 1) {
echo "Your are on page ". $number;
});
Using expressions in place for parameters meant for routing:
Code:
Route::get('/page/{number?}', function ($number = 1) {
echo "Your are on page ". $number;
})->where('number', '[0-9]+');
The naming of the routes:
Code:
Route::post('/submit', 'ContactFormController@submitForm')->name('contact.submit');
Grouping the routes:
Code:
Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});
Another form of the grouping of the routes can look like:
Code:
Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::group(['middleware' => ['auth']], function () {
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});
});
The usage of namespaces:
Code:
Route::group(['namespace' => 'Post'])
// this route group will load all controllers
//from within the "App\Http\Controllers\Post"
Route::group(['namespace' => 'Post','prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::group(['middleware' => ['auth']], function () {
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});
});
If one has to route the cache:
PHP artisan route: clear
PHP artisan route: cache
And finally, if the route has to be debugged:
PHP artisan route: list
Another quick example of route grouping:
Code:
// In Laravel 4.0 your routes would look something like this
Route::get('/', array('as' => 'home', 'uses' => 'Controllers\HomeController@index'));
Route::get('admin/dashboard', array('as' => 'admin.dashboard.index', 'uses' => 'Controllers\Admin\DashboardController@index'));
// etc. This gets messy very quickly.
// Laravel 4.1 allows us to write this a lot cleaner:
Route::group(array('namespace' => 'Controllers'), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'))
Route::group(array('namespace' => 'Admin'), function()
{
// Notice how, by nesting route groups, the namespace will automatically
// be nested as well!
Route::get('admin/dashboard', array('as' => 'admin.dashboard.index', 'uses' => 'DashboardController@index'));
});
});
Examples to Implement Laravel Route
Let us see some examples mentioned:
Example #1
Code:
Route::group([], function()
{
Route::get('/first',function()
{
echo "first route";
});
Route::get('/second',function()
{
echo "second route";
});
Route::get('/third',function()
{
echo "third route";
});
});
Output:
Example #2
Code:
Route::group(['prefix' => 'tutorial'], function()
{
Route::get('/aws',function()
{
echo "aws tutorial";
});
Route::get('/jira',function()
{
echo "jira tutorial";
});
Route::get('/testng',function()
{
echo "testng tutorial";
});
});
Output:
Example #3
Code:
Route::middleware(['age'])->group( function()
{
Route::get('/aws',function()
{
echo "aws tutorial";
});
Route::get('/jira',function()
{
echo "jira tutorial";
});
Route::get('/testng',function()
{
echo "testng tutorial";
});
});
<?php
namespace App\Http\Middleware;
use Closure;
class CheckAge
{
/**
* Handle an incoming request.
*
* @param
\Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//return "middleware";
echo "Hello javaTpoint <br>";
return $next($request);
}
}
Output:
Example #4
Code:
127.0.0.1 localhost
127.0.0.1 fakebook.dev
127.0.0.1 masud.fakebook.dev
127.0.0.1 sohel.fakebook.dev
::1 localhost
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\blog\public"
ServerName fakebook.dev
<directory "C:\xampp\htdocs\blog\public">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny, Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
Route::group(['domain' => 'fakebook.dev'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
Route::group(['domain' => '{username}.fakebook.dev'], function()
{
Route::any('/', function($username)
{
return 'You visit your account: '. $username;
});
});
Output:
Code:
Route::group(['domain' => 'fakebook.dev'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
Route::group(['domain' => '{username}.fakebook.dev'], function()
{
Route::any('/', function($username)
{
return 'You visit your account: '. $username;
});
$data_user = [
'masud' => [
'profile' => ' a cute programmer. ',
'status' => [ 'I\'m cool!', 'I\'m cool very Cool!', 'Fantastic!']
],
'sohel' => [
'profile' => 'a boss programmer.' ,
'status' => [ 'Sweet!', 'Today is incredible!', 'Nice ..']
]
];
Route :: get ( 'profile', function ($username) use ($data_user)
{
return $username." is a ".$data_user[$username] [ 'profile'];
});
});
Output:
Code:
Route::group(['domain' => 'fakebook.dev'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
Route::group(['domain' => '{username}.fakebook.dev'], function()
{
Route::any('/', function($username)
{
return 'You visit your account: '. $username;
});
$data_user = [
'masud' => [
'profile' => ' a cute programmer. ',
'status' => [ 'I\'m cool!', 'I\'m cool very Cool!', 'Fantastic!']
],
'sohel' => [
'profile' => 'a boss programmer.' ,
'status' => [ 'Sweet!', 'Today is incredible!', 'Nice ..']
]
];
Route :: get ('status/{id}', function ($username, $id) use ($data_user)
{
return $username. ' writes: '. $data_user [$username] ['status'] [$id];
});
});
Output:
Example #5
Code:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NewsController extends Controller
{
//
}
Output:
Example #6
Code:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NewsController extends Controller
{
//
}
Route::get('admin/news', [
'uses' => 'Admin\NewsController@index'
]);
Output:
Code:
Route::group(['namespace' => 'Admin'], function()
2
{
3
Route::get('admin/news', [
'uses' => 'NewsController@index'
]);
Route::get('admin/users', [
'uses' => 'UserController@index'
]);
});
Example #7
Code:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function()
{
Route::get('news', [
'uses' => 'NewsController@index'
]);
Route::get('users', [
'uses' => 'UserController@index'
]);
...
});
Output:
Conclusion
The Laravel framework, with evocative and expressive querying, helps the developer create scalable functions that can withstand the rigors of modern-day programming. The route grouping is a great way to keep the code neat and reduce programming time.
Recommended Articles
We hope that this EDUCBA information on “Laravel Route” was beneficial to you. You can view EDUCBA’s recommended articles for more information.