4. Lấy request path (Retrieving the request path)

https://viblo.asia/p/tap-16-request-laravel-aWj534Y8K6m

1. Lấy request path (Retrieving the request path)

Với method path sẽ trả về path của request hiện tại. Nếu request hiện tại có url là http://domain.com/foo/bar thì method path sẽ trả về foo/bar.

use Illuminate\Http\Request;

Route::get('/foo/bar', function (Request $request) {
    return $request->path();
});

Các bạn có thể dùng đoạn code trên để test.

Method is cho phép bạn xác thực rằng request path hiện tại khớp với pattern mà bạn đưa ra. Bạn có thể sử dụng ký tự * để làm đại diện cho các thành phần phía sau.

use Illuminate\Http\Request;

Route::get('/admin/post', function (Request $request) {
    if ($request->is('admin/*')) {
        return 'Request path matches with "admin/*" pattern';
    }
});

Pattern ở đây chính là admin/*, nếu ta truy cập request path admin/post thì chắc chắn sẽ trùng khớp.

Last updated