Laravel - 模型绑定中的重定向执行控制器方法

Laravel - Redirect in Model Binding executes controller method

我有一个列出体育运动员的系统,我在 Laravel 5.2 中使用模型绑定来获取基于 URL 中的 slug 的运动员模型。在某些情况下,球员名字的拼写可能已经改变,所以我有一个中间 table 在那里我寻找旧的 slug,如果找到了用新的 slug 替换它 URL并重定向到新的 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}  

希望有人能帮助我,如果您需要更多信息,请告诉我!

我想是预期的行为。

模型绑定允许您自定义如何return模型中的数据用于那个路由参数(在您的情况下{playerSlug}),但无论如何结束到控制器方法或闭包中。