laravel中的路线问题

时间:2016-02-02 02:23:36

标签: laravel routes

我有这两条路线:

GET|HEAD  | tournaments/{tournament}/users  | tournaments.{tournament}.users.index  | App\Http\Controllers\TournamentUserController@index | 
POST      | tournaments/{tournament}/users  | tournaments.{tournament}.users.store  | App\Http\Controllers\TournamentUserController@store |  

我需要打电话:

    $breadcrumbs->push(trans('crud.add_competitors'), route('tournaments.users.index', $tournament->id));

但我收到错误:

route('tournaments.users.index', $tournament->id)

错误是:

Route [tournaments.users.index] not defined.

拨打这条路线的方法是什么?

5 个答案:

答案 0 :(得分:1)

路由名称是文字的,所以它应该是这样的:

route('tournaments.{tournament}.users.index', $tournament->id)

如果您可以:tournaments.users.index,我建议您更改名称。您的路径名称不需要包含参数。实际上,他们不能在其中加入论据。你的uri,控制器等接受参数。路线名称实际上只是路线的“名称”/直观描述。

答案 1 :(得分:0)

我认为在路线名中使用参数并不好。如果你使用这个

route('tournaments.users.index', ['tournament'=>$tournament->id])

然后,你的路线必须我

Route::get('/tournaments/{tournament}/users',array(
   'as'=>'tournaments.users.index',                     
   'uses'=>'TournamentUserController@index' 
));

答案 2 :(得分:0)

尝试在laravel中实现命名路由的概念:

https://laravel.com/docs/5.2/routing#named-routes

您案件中的路线变为:

Route::get('tournaments/{tournament}/users', ['as' => 'tournaments.users.index', 'uses' => 'TournamentUserController@index']);

现在您可以将此路线称为:

route('tournaments.users.index', ['tournament' => $tournament->id]);

希望这能帮到你!

答案 3 :(得分:-1)

您使用命名路由,但您的命名路由定义为

  tournaments.{tournament}.users.index

但你用

打电话给路线
 route('tournaments.users.index', $tournament->id)

您需要将路线名称更改为tournaments.users.index

答案 4 :(得分:-1)

嘿,如果你有两个控制器就像这样使用

Route::resource('tournaments','TournamentController');
Route::resource('users','UserController');

这将为您提供您所要求的路线:)

相关问题