Skip to Content
PHPCODE
Named Routes & Route Groups in Laravel
laravel code / August 19, 2018

Named Routes & Route Groups in laravel

step 1 :go to routes folder and web.php and write this code
for Example
Route::get(‘/’,function(){
echo url(‘admin’,[234]);
});
or
step 2 :-go to routes folder and web.php and write code

Route::get(‘/admin/{number}’,function($number){
echo “Number passed to closure is $number”;
})->where([‘number’=>”[0-9]+”])->name(‘user-number’);

Route::get(‘/’,function(){
echo route(‘user-number’,[234]);
});

sorting by

Route::get(‘/imtiyaj/php/def’,function($number){
echo “Number passed to closure is $number”;
})->where([‘number’=>”[0-9]+”])->name(‘user-number’);

array

Route::get(‘/’,[
‘as’ => ‘base_url’,
‘uses’ =>  ‘AdminController@index’,
]);

Route Groups

Route::group([‘middleware’=>’web’],function(){
Route::get();
Route::post();
Route::put();
Route::patch();
Route::delete();
});

Route::get(‘admin/dashboard’,’AdminController@dashboard’);
Route::group([‘middleware’=>’web’,’namespace’=>’Admin’,’as’=>’admin-‘],function(){
Route::get(‘dashboard’,[
‘as’ =>’dashboard’,
‘uses’ =>’AdminController@dashboard’,
]);
});
route(‘admin-dashboard’);

prefix used

Route::group([‘prefix’=>’admin-panel’],
function(){
Route::get(‘dashboard’,[
‘as’ =>’dashboard’,
‘uses’ =>’AdminController@dashboard’,
]);
});

Route::get(‘/’,function(){
echo route(‘dashboard’);
});

Domain

Route::group([‘domain’=>'{domain}’],
function(){
Route::get(‘dashboard’,[
‘as’ =>’dashboard’,
‘uses’ =>’AdminController@dashboard’,
]);
});

Route::get(‘/’,function(){
echo route(‘dashboard’);
});

Route::get(‘/’,function(){
echo route(‘user-number’,[‘sortby’=>’username’]);
});

Controller created
<?php

namespace AppHttpControllers;
class AdminController extends Controller {

public function index($number)
{
echo “Number Passed is: $number”;
}
public function dashboard($domain)
{
echo “$domain”;
}

}
?>

Nested Group Created

Route::group([‘prefix’=>’admin-panel’],function(){

Route::group([‘prefix’=>’subscribers’],function(){
Route::get(‘add’,function(){
echo “Add subscribers.”;
})->name(‘add-sub’);
});
});

Route::get(‘/’,function(){
echo route(‘add-sub’);
});

Named Routes & Route Groups

routes group
prefix url
prefix root name
prefix namespace
sub domain

Comments
No comments yet, take the initiative.

Leave a Reply

Your email address will not be published.

PHPCODE © 2023