我有一个列出体育运动员的系统,并且我在Laravel 5.2中使用模型绑定来根据URL中的slug获取播放器模型。在某些情况下,玩家名称的拼写可能已经改变,所以我有一个中间表,在那里我寻找旧的slug,如果找到它,用URL中的新slug替换它并重定向到新的URL。本地工作正常,在我的实时服务器上它也可以工作,但在实时它会记录一个异常,这似乎表明路由的控制器方法甚至在重定向时运行。
路线:
Route::get('{playerSlug}', ['as' => 'player.main', 'uses' => 'PlayersController@player']);
结合:
Route::bind('playerSlug', function($value)
{
$player = Player::getPlayerBySlug($value);
if ( ! $player)
{
return App::abort(404);
}
elseif(is_string($player))
{
return \Redirect::to(URL::to($player), 301)->send();
}
else
{
return $player;
}
});
启动控制器方法:
public function player($wordPlayers, $player, $scope = null)
{
$season = $player->latestSeason();
例外:
[2016-02-19 00:34:56] production.ERROR: BadMethodCallException: Method [latestSeason] does not exist on Redirect. in /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Http/RedirectResponse.php:198
Stack trace:
#0 /home/forge/cuetracker.net/app/Http/Controllers/PlayersController.php(72): Illuminate\Http\RedirectResponse->__call('latestSeason', Array)
#1 [internal function]: App\Http\Controllers\PlayersController->player('Players', Object(Illuminate\Http\RedirectResponse))
#2 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9137): call_user_func_array(Array, Array)
#3 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9199): Illuminate\Routing\Controller->callAction('player', Array)
#4 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9179): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\PlayersController), Object(Illuminate\Routing\Route), 'player')
#5 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#6 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#7 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#8 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9656): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#9 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9180): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#10 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9167): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\PlayersController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'player')
#11 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(8262): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'player')
#12 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(8249): Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))
#13 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(7977): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#14 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#16 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#17 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9656): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#18 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(7978): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#19 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(7969): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#20 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(7959): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#21 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(2381): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#22 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#23 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#24 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(13111): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#25 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9666): call_user_func_array(Array, Array)
#27 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#29 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(11675): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#30 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9666): call_user_func_array(Array, Array)
#32 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#34 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(12850): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#35 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#36 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9666): call_user_func_array(Array, Array)
#37 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#38 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#39 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(12787): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#40 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9666): call_user_func_array(Array, Array)
#42 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#44 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(3183): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#45 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#46 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9666): call_user_func_array(Array, Array)
#47 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#48 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#49 /home/forge/cuetracker.net/app/Http/Middleware/EnableQueryLog.php(18): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#50 [internal function]: App\Http\Middleware\EnableQueryLog->handle(Object(Illuminate\Http\Request), Object(Closure))
#51 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9666): call_user_func_array(Array, Array)
#52 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#53 /home/forge/cuetracker.net/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#54 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#55 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(9656): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#56 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(2328): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#57 /home/forge/cuetracker.net/bootstrap/cache/compiled.php(2312): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#58 /home/forge/cuetracker.net/public/index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#59 {main}
希望有人可以帮助我,如果您需要更多信息,请告诉我们!
答案 0 :(得分:1)
我想是预期的行为。
模型绑定允许您自定义如何从该路由参数(在您的情况下为{playerSlug}
)中返回模型中的数据,但无论如何您最终都会进入控制器方法或闭包。