<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] # 路由 >[success] routes/web.php 默認分配 web 中間件組 > routes/api.php 默認分配 api 中間件組,路由默認添加`api`前綴 > 參看設置文件 app\Providers\RouteServiceProvider.php ## 基本路由 ``` // 路由方法 Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback); // 一個或多個路由方法 Route::match(['get', 'post'], '/', function () { // }); // 所有路由方法 Route::any('foo', function () { // }); // CSRF 保護,web 路由(POST、PUT、DELETE) <form method="POST" action="/profile"> @csrf ... </form> // 重定向路由,,默認會返回狀態碼 302 Route::redirect('/here', '/there'); // 301 Route::redirect('/here', '/there', 301); Route::permanentRedirect('/here', '/there'); // 視圖路由 Route::view('/welcome', 'welcome'); Route::view('/welcome', 'welcome', ['name' => 'Taylor']); // 控制器路由,只需要指定命名空間為`App\Http\Controllers`之后的部分 Route::get('user/{id}', 'UserController@show'); // 完整的控制器類名為 App\Http\Controllers\Photos\AdminController Route::get('foo', 'Photos\AdminController@method'); ``` ## 路由參數 ``` // 必填參數 Route::get('user/{id}', function ($id) { return 'User '.$id; }); Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // }); // 可選參數 Route::get('user/{name?}', function ($name = null) { return $name; }); Route::get('user/{name?}', function ($name = 'John') { return $name; }); // 正則表達式約束 Route::get('user/{name}', function ($name) { // })->where('name', '[A-Za-z]+'); Route::get('user/{id}', function ($id) { // })->where('id', '[0-9]+'); Route::get('user/{id}/{name}', function ($id, $name) { // })->where(['id' => '[0-9]+', 'name' => '[a-z]+']); // 全局約束,在 RouteServiceProvider 的 boot 方法中定義 public function boot() { Route::pattern('id', '[0-9]+'); parent::boot(); } // 編碼正斜杠字符 // 必須使用 where 條件正則表達式顯式地允許 / 成為占位符的一部分 // 注意:僅支持在最后一個參數生效 Route::get('search/{search}', function ($search) { return $search; })->where('search', '.*'); ``` ## 路由命名 ``` Route::get('user/profile', function () { // })->name('profile'); Route::get('user/profile', 'UserProfileController@show')->name('profile'); // 生成指定路由的 URL $url = route('profile'); return redirect()->route('profile'); // 參數設置 Route::get('user/{id}/profile', function ($id) { // })->name('profile'); $url = route('profile', ['id' => 1]); // 檢查當前請求是否指向了某個路由 public function handle($request, Closure $next) { if ($request->route()->named('profile')) { // } return $next($request); } ``` ## 路由組 ``` // 中間件 Route::middleware(['first', 'second'])->group(function () { Route::get('/', function () { // // 使用 first 和 second 中間件 }); Route::get('user/profile', function () { // // 使用 first 和 second 中間件 }); }); // 命名空間 Route::namespace('Admin')->group(function () { // 在 "App\Http\Controllers\Admin" 命名空間下的控制器 Route::get('/manage', 'IndexController@index'); }); // 子域名路由 Route::domain('{account}.myapp.com')->group(function () { Route::get('user/{id}', function ($account, $id) { // }); }); // 路由前綴 Route::prefix('admin')->group(function () { Route::get('users', function () { // 匹配包含 "/admin/users" 的 URL }); }); // 路由名稱前綴 Route::name('admin.')->group(function () { Route::get('users', function () { // 指定路由名為 "admin.users"... })->name('users'); }); ``` ## 路由模型綁定 ``` // 隱式綁定,在數據庫中找不到對應的模型實例,將會自動生成 404 異常 Route::get('api/users/{user}', function (App\User $user) { return $user->email; }); // 自定義路由參數的鍵名,在 Eloquent 模型上重寫 getRouteKeyName 方法 public function getRouteKeyName() { return 'slug'; } // 顯式綁定,在 RouteServiceProvider 類中的 boot 方法內定義這些顯式模型綁定 public function boot() { parent::boot(); Route::model('user', App\User::class); } Route::get('profile/{user}', function ($user) { return $user->email; }); // 自定義邏輯解析 public function boot() { parent::boot(); Route::bind('user', function ($value) { return App\User::where('name', $value)->first() ?? abort(404); }); } // 或者重寫 Eloquent 模型上的 resolveRouteBinding 方法 public function resolveRouteBinding($value) { return $this->where('name', $value)->first() ?? abort(404); } ``` ## 回退路由 >[danger] 注意:回退路由應始終是你應用程序注冊的最后一個路由。 ``` // 執行未匹配的路由 Route::fallback(function () { abort(404); }); ``` ## 訪問控制 ``` // 經過身份驗證并且用戶每分鐘訪問頻率不超過 60 次的路由組 Route::middleware('auth:api', 'throttle:60,1')->group(function () { Route::get('/user', function () { // }); }); // 動態訪問控制 // 根據已驗證的 User 模型的屬性 rate_limit,指定動態請求的最大值。 Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () { Route::get('/user', function () { // }); }); ``` >[success] 使用中間件 auth:api 需要傳遞令牌參數 ## 表單方法偽造 ``` // PUT、PATCH、DELETE <form action="/foo/bar" method="POST"> <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> </form> <form action="/foo/bar" method="POST"> @method('PUT') @csrf </form> ``` ## 訪問當前路由 ``` // 使用 Route facade 上的方法訪問處理傳入請求的路由的信息 $route = Route::current(); $name = Route::currentRouteName(); $action = Route::currentRouteAction(); ``` ## 資源路由 ``` // 生成API資源路由 $ php artisan make:controller PhotoController --resource Route::resource('photos', 'PhotoController'); // 定義多個資源控制器 Route::resources([ 'photos' => 'PhotoController', 'posts' => 'PostController' ]); ``` **資源路由請求方法** HTTP 方法 | URI | 動作 | 路由名稱 ----------|-----------------------|--------------|--------------------- GET | `/photos` | index | photos.index GET | `/photos/create` | create | photos.create POST | `/photos` | store | photos.store GET | `/photos/{photo}` | show | photos.show GET | `/photos/{photo}/edit` | edit | photos.edit PUT/PATCH | `/photos/{photo}` | update | photos.update DELETE | `/photos/{photo}` | destroy | photos.destroy ## API資源路由 >[success] 排除 create 和 edit 顯示 HTML 模板的路由 ``` // 生成API資源路由 $ php artisan make:controller Api/PhotoController --api Route::apiResource('photos', 'Api\PhotoController'); Route::apiResources([ 'photos' => 'PhotoController', 'posts' => 'PostController' ]); ``` ## 路由緩存 >[danger] 注意:基于閉包的路由無法被緩存。要使用路由緩存,需要將閉包路由轉換成控制器路由。 ``` // 生成 $ php artisan route:cache // 清除 $ php artisan route:clear ```
                  <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>

                              哎呀哎呀视频在线观看