Route in Laravel
Source img: http://hieupham.name.vn/quy-trinh-xu-ly-du-lieu-trong-laravel.html 1. The structure of the Route Route::get('home.laravel',...
1. The structure of the Route
Route::get('home.laravel', function(){ });
In which:
get: method (There are many others method such as post,..)
'home/laravel': link
funtion: parameter
In folder routes/web.php
Create a Route
Check:
2. Transmission the parameter on Route
2.1. Transmission the parameter on Route
Route::get('myroute/{name}',function($name){ });
2.2. Set condition for parameter with method where()
Text condition
Route::get('myroute/{name}', function($name){ return "Hello everyone!".$name; })->where (['name'=> '[a-zA-Z]+']);//only transmission text
Number condition
Route::get('myroute/{number}',function($number){ return "Hello everyone!".$number; })->where(['number'=>'[0-9]+']);//only transmission number
For example:
If you import the text, it will not found
And if you import the number, it will complete.
Others case:
- Only transmission number 6-32: 'number'=> '[0-9]{6,32}'
- Only transmission number have 5 characters: 'number'=>'[0-9]{5}'
- Transmission number and text: 'number'=>'[0-9a-zA-Z]+'
- Transmisson number and text, limit in 6 characters: 'number'=>'0-9a-zA-Z]{6}'
3. Identify the route
Call Route by the name, use route('nameroute');
Route::get('myroute', function(){ return redirect()->route('nameroute'); });
Way 1: Declare 'as'=>'nameroute'
Route::get('myroute', ['as'=>'nameroute', function(){ return "Renamed"; }]);
Why must we assign a name to Route? Because call this Route to the others Route.
Way 2: Add method name('nameroute')
Route::get('myroute', function(){ return "Renamed"; }])->name('nameroute');
Similar, when I call CallName=>Enter=> Link will change Route2
4. Route Group
Route::group([ ‘prefix’ => ‘MyGroup’ ] , function(){ //Gọi Route User1: domain/MyGroup/User1 Route::get(‘User1’, function(){ return ‘User1’; }); //Gọi Route User2: domain/MyGroup/User2 Route::get(‘User2’, function(){ return ‘User2’;}); //Gọi Route User3: domain/MyGroup/User3 Route::get(‘User3’, function(){ return ‘User3’; }); });
Khoa học - Công nghệ
/khoa-hoc-cong-nghe
Bài viết nổi bật khác
- Hot nhất
- Mới nhất