一条路线工作,另一条路线不工作

时间:2015-07-06 16:53:31

标签: php laravel

我有这两条路线:

Route::get('delete/{user}', array('as' => 'delete', 'uses' => 'AdminController@getDeleteUser'));
Route::get('delete/{article}', array('as' => 'delete', 'uses' => 'AdminController@getDeleteArticle'));

如果我首先放置'delete/{user}',那么它的工作原理。如果我把它放在“delete/{article}”之下,那么他们就会删除文章。我怎样才能使它们都起作用?

2 个答案:

答案 0 :(得分:1)

您应该重新设计您的API。 看起来,如果你有像

这样的路线,你会做得更好
Route::get('user/delete/{id}', array('as' => 'deleteUser', 'uses' => 
    'AdminController@getDeleteUser'));
    Route::get('article/delete/{id}', array('as' => 'deleteArticle', 'uses' => 'AdminController@getDeleteArticle'));

您有2条具有相同名称的命名路由。

答案 1 :(得分:0)

花括号之间的任何内容都是通配符参数,因此使用' delete / {user}'声明路由。意味着删除/'之后的任何内容将与该路线一样工作,所以要达到你想要的效果

Route::get('delete/user/{id}', array('as' => 'deleteUser', 'uses' => 'AdminController@getDeleteUser'));
Route::get('delete/article/{id}', array('as' => 'deleteArticle', 'uses' => 'AdminController@getDeleteArticle'));

你也可以使用正则表达式来表示同样的路线

Route::get('delete/{user}', function ($name) {
    return "a";
})->where('name', 'regular expression here');
Route::get('delete/{article}', function ($id) {
    return "b";
})->where('id', 'regular expression here');