3 回答

TA貢獻1803條經(jīng)驗 獲得超6個贊
就像
$var = $_GET['hello'];
$var = 'go away';
也會毀了!如果“可變”主題具有“創(chuàng)建”值怎么辦?
要解決這個問題,您需要重新排序您的路線以保留topic關(guān)鍵字
Route::get('/topics/create', 'TopicsController@create')->name('topics.create');
Route::get('/topics/{topic}', 'TopicsController@show')->name('topics.show');
或者通過為您的第一條路線添加一些正則表達式以確保該$topic值不是create
Route::get('/topics/{topic}', 'TopicsController@show')->name('topics.show')->where('topic', '^((?!topic).)*$');

TA貢獻1831條經(jīng)驗 獲得超4個贊
我正在解釋一種在一行中編寫此路由的方法。看起來非常簡單明了。
Route::resource('topics', 'TopicsController')->only([ 'create', 'show' ]);

TA貢獻1864條經(jīng)驗 獲得超6個贊
只是為了擴展已經(jīng)給出的答案,這是完美的。如果您使用資源控制器和Facaderesource
上的方法,Route
您可以看到框架為您處理的路由排序示例。
php artisan make:controller --resource TopicsController
添加
Route::resource('topics', 'TopicsController');
到您的web.php
跑步
php artisan route:list | grep topics
+--------+----------------------------------------+----------------------------------------------------------------+----------------------------------+----------------------------------------------------------------------------------+-------------------------------------------------------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------------------------------------+----------------------------------------------------------------+----------------------------------+----------------------------------------------------------------------------------+-------------------------------------------------------------------------+
| | GET|HEAD | topics | topics.index | App\Http\Controllers\TopicsController@index | web |
| | POST | topics | topics.store | App\Http\Controllers\TopicsController@store | web |
| | GET|HEAD | topics/create | topics.create | App\Http\Controllers\TopicsController@create | web |
| | GET|HEAD | topics/{topic} | topics.show | App\Http\Controllers\TopicsController@show | web |
| | PUT|PATCH | topics/{topic} | topics.update | App\Http\Controllers\TopicsController@update | web |
| | DELETE | topics/{topic} | topics.destroy | App\Http\Controllers\TopicsController@destroy | web |
| | GET|HEAD | topics/{topic}/edit | topics.edit | App\Http\Controllers\TopicsController@edit | web |
使用 Resources,Laravel 會為您處理連接所有必要的路線,您會看到它topics/create在任何路線之前注冊topics/{topic},原因正是在接受的答案中解釋的原因。
- 3 回答
- 0 關(guān)注
- 121 瀏覽
添加回答
舉報