3 回答
TA贡献1803条经验 获得超6个赞
就像
$var = $_GET['hello'];
$var = 'go away';
也会毁了!如果“可变”主题具有“创建”值怎么办?
要解决这个问题,您需要重新排序您的路线以保留topic关键字
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条经验 获得超4个赞
我正在解释一种在一行中编写此路由的方法。看起来非常简单明了。
Route::resource('topics', 'TopicsController')->only([ 'create', 'show' ]);
TA贡献1864条经验 获得超6个赞
只是为了扩展已经给出的答案,这是完美的。如果您使用资源控制器和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 关注
- 88 浏览
添加回答
举报