網址
」基礎
預設值Laravel 提供了幾個輔助函數來為應用程式產生URL。主要用於在模板和 API 回應中建立 URL 或在應用程式的其它部分產生重定向回應。 」基礎輔助函數url
可以用於應用的任何一個URL。產生的 URL 將自動使用目前請求中的方案( HTTP 或 HTTPS )和主機:$post = App\Post::find(1); echo url("/posts/{$post->id}"); // http://example.com/posts/1
存取目前URL
如果沒有給輔助函數url
提供路徑,則會傳回一個Illuminate\Routing\UrlGenerator
實例,來允許你存取有關當前URL 的資訊:
// Get the current URL without the query string... echo url()->current(); // Get the current URL including the query string... echo url()->full(); // Get the full URL for the previous request... echo url()->previous();
上面的這些方法都可以透過URL
facade 來存取:
use Illuminate\Support\Facades\URL; echo URL::current();
#命名路由的URL
輔助函數
route
route 函數呼叫進行任何更改。例如,假設你的應用程式包含以下路由:
Route::get('/post/{post}', function () { // })->name('post.show');要產生此路由的URL,可以像這樣使用輔助函數
route
:echo route('post.show', ['post' => 1]);
// http://example.com/post/1
您通常會使用Eloquent 模型的主鍵產生URL。因此,您可以將 Eloquent 模型作為參數值傳遞。 route 輔助函數將自動擷取模型的主鍵:
echo route('post.show', ['post' => $post]);
輔助函數
route 也可用於為具有多個參數的路由產生URL:Route::get('/post/{post}/comment/{comment}', function () { // })->name('comment.show'); echo route('comment.show', ['post' => 1, 'comment' => 3]); // http://example.com/post/1/comment/3
簽名URL
例如,你可以使用簽名 URL 來實現透過電子郵件發送給客戶的公共 「取消訂閱」連結。若要建立指向路徑的簽章URL,請使用facade 的signedRoute
方法URL
:###use Illuminate\Support\Facades\URL;return URL::signedRoute('unsubscribe', ['user' => 1]);###如果要產生過期的臨時簽章路由URL,可以使用下列## #temporarySignedRoute### 方法:###
use Illuminate\Support\Facades\URL;return URL::temporarySignedRoute( 'unsubscribe', now()->addMinutes(30), ['user' => 1] );#########驗證簽章路由要求######要驗證傳入請求是否具有有效簽名,你應該呼叫###hasValidSignature####傳入的方法###Request###:###
use Illuminate\Http\Request; Route::get('/unsubscribe/{user}', function (Request $request) { if (! $request->hasValidSignature()) { abort(401); } // ... })->name('unsubscribe');###或者,你可以將###Illuminate\Routing\Middleware\ValidateSignature### 中間件指派給路由。如果它不存在,則應該在HTTP 核心的###routeMiddleware### 陣列中為此中間件分配一個鍵:###
/** * 应用程序的路由中间件 * * 这些中间件可能被分配给组或单独使用 * * @var array */ protected $routeMiddleware = [ 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, ];###在核心中註冊中間件後,你可以將其附加到路由中。如果傳入請求沒有有效簽名,則中間件將自動傳回 ###403### 錯誤回應:###
Route::post('/unsubscribe/{user}', function (Request $request) { // ... })->name('unsubscribe')->middleware('signed');##################
控制器行為的 URL
action
功能可以為給定的控制器行為產生 URL。這個功能不需要你傳遞控制器的完整命名空間,但你需要傳遞相對於命名空間 App\Http\Controllers
的控制器類別名稱:
$url = action('HomeController@index');
你也可以使用“可呼叫」 陣列語法參考操作:
use App\Http\Controllers\HomeController; $url = action([HomeController::class, 'index']);
如果控制器方法需要路由參數,那就將它們作為第二個參數傳遞給 action
函數:
$url = action('UserController@profile', ['id' => 1]);
預設值
對於某些應用程序,你可能希望為某些URL 參數的請求範圍指定預設值。例如,假設有些路由定義了 {locale} 參數:
Route::get('/{locale}/posts', function () { //})->name('post.index');
每次都透過 locale
來呼叫輔助函數 route
也是一件很麻煩的事情。因此,使用 URL::defaults
方法定義這個參數的預設值,可以讓該參數始終存在目前請求中。然後就能從 路由中間件 呼叫此方法來存取當前請求:
<?php namespace App\Http\Middleware; use Closure;use Illuminate\Support\Facades\URL; class SetDefaultLocaleForUrls{ public function handle($request, Closure $next) { URL::defaults(['locale' => $request->user()->locale]); return $next($request); } }
一旦設定了locale
參數的預設值,您就不再需要透過輔助函數route
產生URL 時傳遞它的值。