# [laravel][routes]routes & url相關筆記 ###### tags: `laravel`,`routes` ## 基本用法 ### 自訂狀態碼 網址範例: http://ur_domain_name/hello ```php= // routes/web.php Route::get('/hello',function(){ // return '<h3>hello laravel</h3>'; // return response('hello laravel',404); return response('<h1>hello</h1>',200) }); ``` ### 自訂header 網址範例: http://ur_domain_name/hello ```php= <?php // routes/web.php Route::get('/hello',function(){ return response('<h1>hello</h1>') ->header('content-type','text/plain') ->header('foo','bar'); }); Route::get('/json',function(){ $output = json_encode(['a'=>1]); return response($output) ->header('content-type','application/json'); }); ``` ### 傳入參數 網址範例: http://ur_domain_name/post/123 ```php= <?php // routes/web.php // ... // 參數必填 Route::get('/post/{id}',function($id){ //dd($id); // dd:dump,die //ddd($id);// ddd:dump,die,debug return response('post ' . $id); }); // 參數選填 Route::get('/post/{id?}', function ($id = '') { return response('post' . $id); }); // 限制參數為數字 Route::get('/post/{id}', function ($id) { return response('post' . $id); })->where('id', '[\d]+'); ``` ### 取得request資料 ```php= <?php Route::get('/search', function (Request $request) { // dd($request) return $request->name; }); ``` ### 傳資料到view ```php= <?php Route::get('/listings', [ 'heading'=>'latest heading' ]); ``` ### 路由model綁定(route model binding) 無route model binding ```php= <?php use App\Models\Listing; Route::get('/id/{id}',function($id){ $listing = Listing::find($id); if($listing){ return view('listing', ['listing'=>$listing] ); }else{ abort('404'); }; }); ``` 使用route model binding ```php= <?php use App\Models\Listing; // route model binding // 以下語法等同於上面那段語法 // 但記得要引用相對應的model Route::get('/id/{listing}',function(Listing $listing){ return view('listing',[ 'listing'=>$listing ]); }); ``` (**重要**)使用route model binding之後,在view中跟該route有關的link全都要改, 假設原本在blade是寫: ```c! <a href={{ route('listings.show',['id'=>$listing->id]) }}>{{ $listing->title }}</a> ``` 要改成: ```c! <a href={{ route('listings.show',['listing'=>$listing]) }}>{{ $listing->title }}</a> ``` 若不改會出現類似以下錯誤: ```c! Illuminate\Routing\Exceptions\UrlGenerationException Missing required parameter for [Route: listings.show] [URI: listings/id/{listing}] [Missing parameter: listing]. (View: C:\xampp\htdocs\laragigs\resources\views\components\listing-card.blade.php) ``` ### route group #### 基本用法 ```php! <?php Route::group([ 'middleware' => 'api', 'prefix' => 'todo' ], function ($router) { Route::get('/all', [TodoController::class, 'all']); Route::post('/select/{id}', [TodoController::class, 'select']); }); ``` ### query string 相關 #### 在blade view中取得query string [參考資料](https://stackoverflow.com/questions/31324801/lumen-get-url-parameter-in-a-blade-view) ```php= @php // $src=app('request')->input('src'); // $src=request()->input('src'); $src= Request::get('src'); dd($src); @endphp ``` #### 取得query string 網址範例: http://ur_domain_name/search?name=John&city=Doe ```php= // routes/web.php use Illuminate\Http\Request; .... Route::get('search', function (Request $request) { // dd($request->name); return $request->name . ' ' . $request->city; }); ``` #### 確認網址是否含有某query string [參考資料](https://stackoverflow.com/questions/38737019/laravel-get-query-string) 網址範例: http://ur_domain_name/query?a=123 可嘗試將a改成其他來查看結果有何不同 ```php= // routes/web.php use Illuminate\Http\Request; .... Route::get('/query',function(Request $request){ if($request->has('a')){ return $request->a; } return 'a does not exists'; }); ``` ### 回傳json 網址範例: http://ur_domain_name/api/posts ```php= //route/api.php Route::get('/posts',function(){ return response()->json([ ['title'=>'post1','content'=>'some content here'], ['title'=>'post2','content'=>'some other content here'], ['title'=>'post3','content'=>'another content here'], ]); }); ``` --- ## 轉址 https://laravel.com/docs/5.8/redirects ### 轉向named routes ```php return redirect()->route('login'); ``` --- ## 未分類 ### route()帶參數 ```php route('home',['id'=>2]) ``` ### 取得目前routes的名稱 https://www.udemy.com/course/projects-in-laravel-learn-laravel-building-10-projects/learn/lecture/16259274?start=570#overview https://stackoverflow.com/questions/30046691/laravel-how-to-get-current-route-name-v5-v6 ```php Request::route()->getName() ``` ### 取得目前url ```php! url()->current() ``` ### 引用public資料夾內的資源(css,js等) >根目錄是從public開始算起 >用url也可以 ```htmlmixed! <!-- css --> <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" > <link rel="stylesheet" href="{{ asset("path/to/urfile.css") }}"> <!-- js --> <script type="text/javascript" src="{{ asset('js/app.js') }}"></script> <script src="{{ asset("path/to/urfile.js") }}"></script> ``` ### 一些判定url及route的方式 ```php! url()->current() === route('home') Request::route()->getName() === 'home' ? "active" : "" Request::is('/') ``` ### 建立resource routes web.php ```php! Route::resource('route_name', 'NameOfController'); ``` --- ## 問題與處理方式 ### 問題: 單一route出現403錯誤訊息 https://stackoverflow.com/questions/24974578/single-route-giving-a-403-forbidden-error-in-laravel-4 此問題可能是因為public資料夾內有跟route同名的資料夾,改一下名稱即可. ### 問題: laravel Method Illuminate\Routing\Route::get does not exist https://stackoverflow.com/questions/32285647/call-to-undefined-method-illuminate-routing-routeget ```php // 把這行註解 // use Illuminate\Routing\Route; // 加上這行 use Illuminate\Support\Facades\Route; ```