Routing
What is Route?

In Laravel All URL maintain by Route.

In Laravel5.6 routes folder available in root folder, we can see there are four file for route that is

web.php - To create website url
api.php - To create api url
channels.php - To create channels url
console.php - To create console url


web.php

By default web.php show this route code.

Route::get('/', function () {
return view('welcome');
});

here first parameter is url(index.php page) and second parameter is closer function (to open welcome page), we can see welcome page in resources->views->welcome.blade.php

Create Route with closer function

Route::get('/about', function () {
return view('welcome');
});

Here first parameter is url(about) and second parameter is open welcome page

Create Route with controller and function

Route::get('/test', 'UserController@dashboard');

Here first parameter is url(test) and second parameter we call dashboard function from user controller

Route with Parameters

Route::get('/aboutus/{id}', function  ($id) {
echo $id;
});

Here first parameter is aboutus url with id and second parameter closer function to print id get from url.

Route with Optional Parameters

Route::get('/user/{username?}', function ($username = 'shishirphpdeveloper') {
return $username;
});

Here first is user url with username is optional paramater showing by (?) marks and second is closer function that accpet parameter and print data.

Named Route

Route::get('/showuser/profile', 'UserController@showProfile')->name('profile');

Route::get('/show',function(){
echo route('profile');
});

Here first route is named route and second is call name route by name profile.

Route with where clause

Route::get('/testpage/{number}', function ($number) {
echo $number;
})->where(['number'=>"[0-9]+"]);


This Route generate URL with numeric parameter like
testpage/45 like this if we take charater etc then this url generate error

Group and Prefix

Route::group(['prefix'  =>  'user','before'  => 'no_auth', 'middleware'  => 'locale'], function(){
Route::get('privacy-policy', 'HomeController@privacyPolicy');
Route::get('faq', 'HomeController@faq');
Route::get('aboutus', 'HomeController@aboutus');
});

suppose for login user has some pages, so this is need to authentic for every page, so here group is very usefull, for this purpose we create all route of user in group.

and use of prefix is that,we use every url like this

user/privacy-policy
user/faq
user/aboutus

Get, Post and Match

Route::post('change_password', 'VendorUserController@changePassword');
Route::get('profile', 'VendorUserController@profile');

Route::match(array('GET', 'POST'), 'add_category', 'AdminCategoryController@add');


Previous
Next