<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # URL ## 基礎 ``` // 生成基礎 URL $post = App\Post::find(1); echo url("/posts/{$post->id}"); // http://example.com/posts/1 // 訪問當前 URL echo url()->current(); //不包含查詢參數 echo url()->full(); //包含查詢參數 echo url()->previous(); //上一個請求連接,包含查詢參數 // 通過 URL facade 訪問 use Illuminate\Support\Facades\URL; echo URL::current(); ``` ## 命名路由的 URL 輔助函數`route`使用命名路由生成 URL,不與路由上定義的 URL 相耦合。 ``` Route::get('/post/{post}', function () { // 單個參數 })->name('post.show'); echo route('post.show', ['post' => 1]); // http://example.com/post/1 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 // 使用 Eloquent 模型 的主鍵生成 URL,自動提取模型的主鍵 echo route('post.show', ['post' => $post]); ``` ### 簽名 URL ``` // http://test.com/unsubscribe/1?signature=簽名 use Illuminate\Support\Facades\URL; return URL::signedRoute('unsubscribe', ['user' => 1]); // 設置有效期 // http://test.com/unsubscribe/1?expires=1608476953&signature=簽名 return URL::signedRoute('unsubscribe', ['user' => 1], now()->addMinutes(30)); // 使用 temporarySignedRoute 方法 return URL::temporarySignedRoute( 'unsubscribe', now()->addMinutes(30), ['user' => 1] ); ``` ### 驗證簽名路由請求 一、Request 驗證 ``` use Illuminate\Http\Request; Route::get('/unsubscribe/{user}', function (Request $request) { if (! $request->hasValidSignature()) { abort(401); } // ... })->name('unsubscribe'); ``` 二、中間件驗證 ``` // 在 app/Http/Kernel.php 中的 $routeMiddleware 屬性中增加中間件,默認已存在 protected $routeMiddleware = [ // ... 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, ]; // 驗證失敗返回403錯誤響應 Route::post('/unsubscribe/{user}', function (Request $request) { // ... })->name('unsubscribe')->middleware('signed'); ``` ## 控制器行為的 URL ``` $url = action('HomeController@index'); use App\Http\Controllers\HomeController; $url = action([HomeController::class, 'index']); // 傳遞參數 $url = action('UserController@profile', ['id' => 1]); ``` ## 默認值 [參考文章](https://learnku.com/articles/43638) ``` // 定義路由 Route::get('/{locale}/posts', 'IndexController@posts')->name('posts'); // 定義中間件 <?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\URL; class SetDefaultLocaleForUrls { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { URL::defaults(['locale' => $request->user()->locale ?? 'cn']); return $next($request); } } // 注冊中間件,在 app/Http/Kernel.php 中的 $routeMiddleware 屬性中增加中間件 protected $routeMiddleware = [ // ... 'locale' => \App\Http\Middleware\SetDefaultLocaleForUrls::class, ]; // 在路由或控制器中配置中間件 Route::get('/', 'IndexController@index')->middleware('locale'); public function __construct() { $this->middleware('locale')->only('index'); } // 在控制器中使用,不需要設置 locale 參數 // http://test.com/cn/posts public function index() { return route('posts'); } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看