gpt4 book ai didi

php - Laravel 4 路由组前缀中的变量

转载 作者:行者123 更新时间:2023-12-02 17:42:29 25 4
gpt4 key购买 nike

给定以下路由,它将响应

http://example.com/game/stats/123
http://example.com/game/stats/game/123
http://example.com/game/stats/reviewer/123

我想知道的是,如何让它响应

http://example.com/game/123/stats
http://example.com/game/123/stats/game
http://example.com/game/123/stats/reviewer

我试过

Route::group(['prefix' => 'game/{game}'], function($game){

但是失败并显示“{closure}() 缺少参数 1”

请注意,除了统计数据之外还有其他四个组,但为了简洁起见,我在这个示例中省略了它们。

Route::group(['prefix' => 'game'], function(){
Route::group(['prefix' => 'stats'], function(){
Route::get('/{game}', ['as' => 'game.stats', function ($game) {
return View::make('competitions.game.allstats');
}]);
Route::get('game/{game}', ['as' => 'game.stats.game', function ($game) {
return View::make('competitions.game.gamestats');
}]);
Route::get('reviewer/{game}', ['as' => 'game.stats.reviewer', function ($game) {
return View::make('competitions.game.reviewstats');
}]);
});
});

最佳答案

你能试试这段代码看看它是否是你想要的。这里的第二组路线只是 {gameId},然后是包含所有其他路线的 stats 组。

Route::group(['prefix' => 'game'], function(){
Route::group(['prefix' => '{gameId}'], function(){
Route::group(['prefix' => 'stats'], function(){
Route::get('/', ['as' => 'game.stats', function ($game) {
return View::make('competitions.game.allstats');
}]);
Route::get('game', ['as' => 'game.stats.game', function ($game) {
return View::make('competitions.game.gamestats');
}]);
Route::get('reviewer', ['as' => 'game.stats.reviewer', function ($game) {
return View::make('competitions.game.reviewstats');
}]);
});
});
});

然后在您的 View 中,您可以通过路由名称调用它们并将 gameId 传递给该路由;

{{ link_to_route('game.stats','All Stats',123) }}  // game/123/stats/
{{ link_to_route('game.stats.game','Game Stats',123) }} // game/123/stats/game
{{ link_to_route('game.stats.reviewer','Review Stats',123) }} // game/123/stats/reviewer

希望这有助于解决您的问题。

编辑

我刚刚检查过它也应该与 Route::group(['prefix' => 'game/{game}' 一起使用,正如您所尝试的那样,但请确保传递 game 参数在创建如上所述的路线时。如果你有更多的变量要传递,你可以传递一个数组给函数。

{{ link_to_route('game.stats','All Stats',['game' => '123','someOtherVar' => '456']) }}

关于php - Laravel 4 路由组前缀中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18976029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com