Laravel có rất nhiều tính năng giúp việc phát triển trở nên dễ dàng hơn. Một trong những tính năng này là liên kết Mô hình tuyến đường, vì vậy thay vì sử dụng khóa chính của bảng để tìm nạp dữ liệu của một mục, chúng ta có thể tận dụng lợi thế của liên kết mô hình tuyến đường như bên dưới
Hãy chứng minh điều này bằng một ví dụ. Hãy xem xét chúng tôi có một bảng bài viết và chúng tôi có một mô hình bài đăng. Để hiển thị bài đăng theo id, chúng ta có thể làm như thế này
// the route parameter is the id of the post // for example //example.com/posts/53 Route::get['posts/{id}', function [$id] { // we have to find the post using the $id $post = Post::find[$id]; // if there is no post, 404 if [!$post] return abort[404]; // return the view and the post return view['post.show', compact['post']]; }];
Trong đoạn mã trên, trước tiên, chúng tôi truy vấn bài đăng theo id, sau đó kiểm tra xem nó đã tồn tại chưa, cuối cùng chúng tôi đã hiển thị chế độ xem
Để đơn giản hóa mọi thứ hơn một chút
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];
Nhưng với Route Model Binding, chúng ta có thể đơn giản hóa việc này như hình bên dưới
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];
Vì vậy, trong đoạn mã trên, Laravel sẽ đưa một mô hình // by using $post, we can inject the Post object
Route::get['posts/{post}', function [$post] {
// we now have access to the $post object! no code necessary
// return the view and the post
return view['post.show', compact['post']];
}];
4 vào bất kỳ bộ điều khiển tuyến đường nào có tham số
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];5 được đính kèm với nó.
Các loại liên kết mô hình tuyến đường
- Ràng buộc mô hình ngầm định
- Ràng buộc mô hình rõ ràng
Ràng buộc mô hình ngầm định
Chỉ cần gõ một tham số trong route Closure [hoặc phương thức điều khiển của bạn] và đặt tên cho tham số giống như tham số route, và nó sẽ tự động coi nó như một liên kết mô hình route
Route::get['posts/{post}', function [App\Post $post] { // be awesome. enjoy having the $post object }];
Laravel đủ thông minh để biết rằng vì một mô hình
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];0 đang được đưa vào phần đóng của bộ điều khiển, nên nó sẽ lấy tham số
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];1 từ tuyến đường và lấy thông tin chi tiết cho người dùng
Bây giờ bạn có thể truy cập bài đăng dưới dạng http. //thí dụ. com/bài viết/3
Nhưng nếu bạn muốn sử dụng khóa định tuyến khác với id thì sao?. Bạn có thể làm điều này thông qua
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];2 trong mô hình Eloquent
// here we used a slug instead of the // id in the route model binding class Post extends Model { public function getRouteKeyName[] { return 'slug'; } }
Sau đó, chúng tôi có thể truy cập tuyến đường của mình bằng cách sử dụng
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];3 thay vì
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];4
Ràng buộc mô hình rõ ràng
Bạn phải nói rõ ràng với laravel rằng bạn muốn nó liên kết một tham số url với một mô hình cụ thể. Có hai cách để thực hiện việc này, chúng ta có thể liên kết tham số với mô hình bằng cách sử dụng mặt tiền
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];5 được cung cấp hoặc thực hiện liên kết này trong
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];6
Sử dụng Mặt tiền // the route parameter is the id of the post
// for example //awesome.dev/posts/53
Route::get['posts/{id}', function [$id] {
// find the post or 404 if not found
$post = Post::findOrFail[$id];
// return the view and the post
return view['post.show', compact['post']];
}];
5
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];3
Sử dụng // the route parameter is the id of the post
// for example //awesome.dev/posts/53
Route::get['posts/{id}', function [$id] {
// find the post or 404 if not found
$post = Post::findOrFail[$id];
// return the view and the post
return view['post.show', compact['post']];
}];
8
Sự khác biệt duy nhất giữa việc sử dụng mặt tiền
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];5 và lớp
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];8 là – việc đăng ký các ràng buộc của bạn được thực hiện trong phương thức
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];1 của lớp
// the route parameter is the id of the post // for example //awesome.dev/posts/53 Route::get['posts/{id}', function [$id] { // find the post or 404 if not found $post = Post::findOrFail[$id]; // return the view and the post return view['post.show', compact['post']]; }];8 [vị trí là thư mục
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];3] và phương thức
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];4 được gọi trên đối tượng
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];5 được đưa vào phương thức
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];2
Ngoại lệ tùy chỉnh cho ràng buộc mô hình tuyến đường
Bạn cũng có thể tùy chỉnh các ngoại lệ mà các liên kết mô hình định tuyến đưa ra [ví dụ: nếu chúng không thể tìm thấy phiên bản của mô hình đó] bằng cách chuyển một Đóng làm tham số thứ ba
// by using $post, we can inject the Post object Route::get['posts/{post}', function [$post] { // we now have access to the $post object! no code necessary // return the view and the post return view['post.show', compact['post']]; }];3
Phần kết luận
Như bạn đã thấy trong hướng dẫn này, Ràng buộc mô hình định tuyến giúp bạn không phải viết nhiều mã mà bạn phải lặp lại hàng ngày trong các hành động tìm nạp một bản ghi duy nhất, nó cũng làm cho mã dễ đọc và có tổ chức hơn