- vừa được xem lúc

Routing

0 0 9

Người đăng: Ngô Quốc Hùng

Theo Viblo Asia

Basic Routing

use Illuminate\Support\Facades\Route; Route::get('/greeting', function () { return 'Hello World';
});

The Default Route Files

  • Tất cả các route đã định nghĩa được tải tựu động qua App\Providers\RouteServiceProvider
  • routes/web.php định nghĩa các route cho web interface, các route này được lọc bởi web middleware group trong file app/Http/Kernel.php và được bảo vệ bởi CSRF
  • route/api.php định nghĩa các rote api và phụ thuộc vào api midddleware group và URI prefix là /api
 protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:60,1', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], ];
  • VD route
use App\Http\Controllers\UserController; Route::get('/user', [UserController::class, 'index']);

Available Router Methods

  • Laravel hỗ trợ 6 phương thức
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
  • Đôi khi 1 route bạn có thể khai báo nhiều method như:
Route::match(['get', 'post'], '/', function () { //
});
  • Hoặc route có thể bất kỳ method nào
Route::any('/', function () { //
});
  • Chú ý: Khi định nghĩa nhiều route chung 1 URI thì các route get, post, put, patch, delete, and options phải được định nghĩa trước các route như: match, any hoặc redirect để đảm bảo request đến route chính xác.

Dependency Injection

use Illuminate\Http\Request; Route::get('/users', function (Request $request) { // ...
});

CSRF Protection

  • Mọi biểu mẫu HTML trỏ đến các tuyến POST, PUT, PATCH hoặc DELETE được xác định trong tệp route web phải bao gồm trường mã thông báo CSRF nếu không các reuqest sẽ bị từ chối
<form method="POST" action="/profile"> @csrf ...
</form>

Redirect Routes

  • Bạn có thể dịnh nghĩa redirect route VD: /here -> /there
Route::redirect('/here', '/there');
  • Route::redirect trả về mã trạng thái 302. Bạn có thể tùy chỉnh mã trạng thái bằng tham số thứ ba tùy chọn:
Route::redirect('/here', '/there', 301);
  • Hoặc cũng có thể sử dụng method permanentRedirect để trả về status code 301:
Route::permanentRedirect('/here', '/there');

View Routes

  • Nếu bạn cần định nghĩa route chỉ có view không cần thông qua Controller thì bạn có thể dùng method view:
Route::view('/welcome', 'welcome'); Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

The Route List

  • Để xem tất cả các route đã được định nghĩa thì có thể dùng command:
php artisan route:list

  • Theo mặc định route middleware sẽ không hiển thị trong output, nếu bạn muốn hiển thị có thể thêm optional -v:
php artisan route:list -v

  • Chỉ định URI cụ thể:
php artisan route:list --path=api

  • Bỏ qua route của thư viện (third-party packages):
php artisan route:list --except-vendor

  • Chỉ định hiển thị duy nhất route của third-party packages:
php artisan route:list --only-vendor

Route Parameters

Required Parameters

  • Tên của tham số phải được dặt trong dấu {}, tên của tham số bao gồm: chữ, số, dấu _
Route::get('/user/{id}', function ($id) { return 'User '.$id;
});
Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) { //
});

Parameters & Dependency Injection

use Illuminate\Http\Request; Route::get('/user/{id}', function (Request $request, $id) { return 'User '.$id;
});

Optional Parameters

Route::get('/user/{name?}', function ($name = null) { return $name;
}); Route::get('/user/{name?}', function ($name = 'John') { return $name;
});

Regular Expression Constraints

  • Nếu request không khớp với ràng buộc route thì sẽ return 404
Route::get('/user/{name}', function ($name) { //
})->where('name', '[A-Za-z]+'); Route::get('/user/{id}', function ($id) { //
})->where('id', '[0-9]+'); Route::get('/user/{id}/{name}', function ($id, $name) { //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
  • Có thể sử dụng các hàm mặc định:
Route::get('/user/{id}/{name}', function ($id, $name) { //
})->whereNumber('id')->whereAlpha('name'); Route::get('/user/{name}', function ($name) { //
})->whereAlphaNumeric('name'); Route::get('/user/{id}', function ($id) { //
})->whereUuid('id'); Route::get('/user/{id}', function ($id) { //
})->whereUlid('id'); Route::get('/category/{category}', function ($category) { //
})->whereIn('category', ['movie', 'song', 'painting']);

Global Constraints

  • Nếu muốn ràng buộc cho tất cả các route khi sử dụng cùng 1 tham số bạn cần thêm ràng buộc vào function boot() trong App\Providers\RouteServiceProvider:
/** * Define your route model bindings, pattern filters, etc. * * @return void */
public function boot()
{ Route::pattern('id', '[0-9]+');
}
  • Khi đó các route dùng tham số id sẽ cần phải tuân thủ ràng buộc trên, vd:
Route::get('/user/{id}', function ($id) { // Only executed if {id} is numeric...
});

Named Routes

  • Tên của route là unique
  • Chúng ta có thể đặt tên cho route:
Route::get('/user/profile', function () { //
})->name('profile');
  • Tên cho controller
Route::get( '/user/profile', [UserProfileController::class, 'show']
)->name('profile');

Generating URLs To Named Routes

  • Khi đã định nghĩa tên route bạn có thể tạo URL, redirect theo tên route
// Generating URLs...
$url = route('profile'); // Generating Redirects...
return redirect()->route('profile'); return to_route('profile');
  • Trong trường hợp sử dụng tham số:
Route::get('/user/{id}/profile', function ($id) { //
})->name('profile'); $url = route('profile', ['id' => 1]); Route::get('/user/{id}/profile', function ($id) { //
})->name('profile'); $url = route('profile', ['id' => 1, 'photos' => 'yes']); // /user/1/profile?photos=yes

Inspecting The Current Route

  • Kiểm tra request hiện tại có khớp với 1 tuyến đường đã được định nghĩa hay không thông qua tên của route có thể sử dụng, ví dụ bạn có thể check thông qua middleware:
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */
public function handle($request, Closure $next)
{ if ($request->route()->named('profile')) { // } return $next($request);
}

Route Groups

  • Bạn có thể nhóm các route theo middleware, Controller, domain

Middleware

Route::middleware(['first', 'second'])->group(function () { Route::get('/', function () { // Uses first & second middleware... }); Route::get('/user/profile', function () { // Uses first & second middleware... });
});

Controllers

use App\Http\Controllers\OrderController; Route::controller(OrderController::class)->group(function () { Route::get('/orders/{id}', 'show'); Route::post('/orders', 'store');
});

Subdomain Routing

Route::domain('{account}.example.com')->group(function () { Route::get('user/{id}', function ($account, $id) { // });
});

Route Prefixes

  • Route prefix method được sử dụng để thêm tiền tố cho mỗi route trong nhóm các route
Route::prefix('admin')->group(function () { Route::get('/users', function () { // Matches The "/admin/users" URL });
});

Route Name Prefixes

  • Tương tự ta cũng có thể prefix theo name route
Route::name('admin.')->group(function () { Route::get('/users', function () { // Route assigned name "admin.users"... })->name('users');
});

Route Model Binding

  • Thông thường chúng ta hay truyền tham số ví dụ như id để lấy ra model tương ứng với id, thay vì truyền id ta có thể truyền 1 instance model tương ứng của id
  • Nếu không có mô hình nào khớp với mô hình truyền vào thì mã lỗi 404 sẽ được trả về

Implicit Binding

use App\Http\Controllers\UserController;
use App\Models\User; // Route definition...
Route::get('/users/{user}', [UserController::class, 'show']); // Controller method definition...
public function show(User $user)
{ return view('user.profile', ['user' => $user]);
}

Soft Deleted Models

  • Thông thường, liên kết mô hình tiềm ẩn sẽ không truy xuất các mô hình đã bị xóa mềm, tuy nhiên bạn có thể truy xuất mô hình đã bị xóa mềm bằng phương thức withTrashed()
use App\Models\User; Route::get('/users/{user}', function (User $user) { return $user->email;
})->withTrashed();

Explicit Binding

Form Method Spoofing

  • Html form không hỗ trỡ các method: PUT, PATCH, DELETE nên khi muốn sử dụng các method trên ta cần sử dụng như sau:
<form action="/example" method="POST"> <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
  • Có thể sử dụng Blade directive
<form action="/example" method="POST"> @method('PUT') @csrf
</form>

Accessing The Current Route

  • Check thông tin route hiện tại:
use Illuminate\Support\Facades\Route; $route = Route::current(); // Illuminate\Routing\Route
$name = Route::currentRouteName(); // string
$action = Route::currentRouteAction(); // string

Cross-Origin Resource Sharing (CORS)

Route Caching

  • Khi triển khai môi trường sản xuất nên sử dụng route cache để tăng performance
  • Khi sử chạy command artisan route:cache thì sẽ sinh ra 1 file bootstrap/cache/routes-v7.php, laravel thay vì đọc file trong folder route thì sẽ đọc file cache thay thế
php artisan route:cache
  • Clear cache thì file bootstrap/cache/routes-v7.php sẽ bị xóa
php artisan route:clear

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tìm hiểu về Resource Controller trong Laravel

Giới thiệu. Trong laravel, việc sử dụng các route post, get, group để gọi đến 1 action của Controller đã là quá quen đối với các bạn sử dụng framework này.

0 0 335

- vừa được xem lúc

Phân quyền đơn giản với package Laravel permission

Như các bạn đã biết, phân quyền trong một ứng dụng là một phần không thể thiếu trong việc phát triển phần mềm, dù đó là ứng dụng web hay là mobile. Vậy nên, hôm nay mình sẽ giới thiệu một package có thể giúp các bạn phân quyền nhanh và đơn giản trong một website được viết bằng PHP với framework là L

0 0 421

- vừa được xem lúc

Sử dụng Swagger để xây dựng API documentation

Giới thiệu về Swagger. RESTful API là một tiêu chuẩn dùng trong việc thiết kế API cho các ứng dụng web (thiết kế Web services) để tiện cho việc quản lý các resource.

0 0 1k

- vừa được xem lúc

Ví dụ CRUD với Laravel và Vuejs.

1. Cài đặt Laravel. composer create-project --prefer-dist laravel/laravel vuelaravelcrud. .

0 0 141

- vừa được xem lúc

Một số tips khi dùng laravel (Part 1)

1. Show database query in raw SQL format. DB::enableQueryLog(); // Bật tính năng query logging. DB::table('users')->get(); // Chạy truy vấn bạn muốn ghi log.

0 0 69

- vừa được xem lúc

Inertiajs - Xây dựng Single Page App không cần API

Tiêu đề là mình lấy từ trang chủ của https://inertiajs.com/ chứ không phải mình tự nghĩ ra đâu nhé :v. Lâu lâu rồi chưa động tới Laravel (dự án cuối cùng mình code là ở ver 5.8), thế nên một ngày đẹp trời lượn vào đọc docs ver 8.

0 0 227