Basic URL:
$post = App\Models\Post::find(1);
echo url("/posts/{$post->id}");Tiếp cận URL hiện tại:
Nếu không cung cấp path cho url, 1 instance của Illuminate\Routing\UrlGenerator được trả về cho phép bạn tiếp cận url:
// không tham số query string
echo url()->current();
// bao gồm cả query string
echo url()->full();
// full url của request trước
echo url()->previous();
// FACADE:
use Illuminate\Support\Facades\URL;
echo URL::current();URL cho route đã đặt tên:
Route::get('post/{post}', function(Post $post) {
// ...
})->name('post.show');Để tạo URL:
echo route('post.show', ['post' => 1]);
// example.com/post/1Nhiều tham số:
Route::get('post/{post}/comment/{comment}', function(Post $post, Comment $comment) {
// ...
})->name('comment.show');
echo route('comment.show', ['post' => 1, 'comment' => 3]);Tham số thừa trong mảng sẽ chuyển thành query string:
route('post.show', ['post' => 1, 'search' => 'rocket']);
// example.com/post/1?search=rocketEloquent model: bạn có thể add Eloquent model làm tham số:
route('post.show', ['post' => $post]);URL cho controller action:
$url = action([HomeController::class, 'index']);
// có thể nhận tham số:
$url = action([HomeController::class, 'index'], ['id' => 1, 'name' => 'David']);Signed URL:
Laravel cho phép bạn tạo URL chứng thực cho route đã đặt tên. Những URL này có mã hash chứng thực trong query string, cho phép Laravel xác minh URL có hợp lệ hay không. Ví dụ tạo 1 URL chứng thực để unsubscribe email cho user. Sử dụng method signedRoute của URL facade:
use Illuminate\Support\Facades\URL;
return URL::signedRoute('unsubscribe', ['user' => 1]);Tạo 1 URL có thời gian hết hạn, sử dụng method temporarySignedRoute. Lưu ý timestamp hết hạn được encode vào signed URL chưa trôi qua:
return URL::temporarySignedRoute(
'unsubscribe', now()->addMinutes(30), ['user' => 1]
);Xác minh URL xác thực:
Để xác minh URL có chữ ký hợp lệ, gọi method hasValidSignature từ Request instance:
Route::get('/unsubscribe/{user}', function(Request $request) {
if (!$request->hasValidSignature())
abort(401);
// code here
})->name('unsubscribe');Thi thoảng bạn có thể append data vào URL xác thực, ví dụ phân trang. Bạn cần chỉ định tham số query sẽ được bỏ quả khi xác thực URL sử dụng method hasValidSignatureWhileIgnoring. Tham số được bỏ qua cho phép user có thể thay đổi những tham số này trên request:
if (! $request->hasValidSignatureWhileIgnoring(['page', 'order'])) {
abort(401);
}Thay vì validate URL chứng thực sử dụng instance của request, bạn có thể sử dụng middleware trong route. Bạn có thể gán middleware này 1 alias trong kernel:
protected $middlewareAliases = [
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];Khi đã register trong kernel, bạn có thể gắn nó trong route. Nếu request đến không có chữ ký hợp lệ, middleware sẽ trả về 403:
Route::post('/unsubscribe/{user}', function(Request $request) {
// ...
})->name('unsubscribe')->middleware('signed');Khi truy cập URL xác thực đã hết hạn, user nhận được 403 code. Bạn có thể customize hành vi này:
public function register(): void
{
$this->renderable(function(InvalidSignatureException $e) {
return response()->view('error.link-expired', [], 403);
});
}