Route Model Binding Not Firing

可紊 提交于 2019-12-24 15:50:37

问题


I have some route/model bindings setup. Around ten of them for various ids. Nothing special going on:

$router->get('/notifications/{active_notification_id}/open', 'NotificationsController@open');

$router->bind('active_notification_id', function ($id)
{
    echo 'here'; echo $id; exit;

    // code
});

The binding is not firing at all. Works fine in eight others but for two of them it just doesn't fire. It goes straight to the controller with an empty model which than crashes my code.

The crazier thing is they ALL work on my local box just fine (Windows) but only having this issue on server (Ubuntu). My php versions are off by just one minor version. But 8 of the bindings work, it's just those two simply won't fire.

Anyone have an idea?

  • Note my Laravel and package versions are the same on both ends.

UPDATE: So actually it seems none of my routes will echo out on production. I "assumed" the others were working because they worked correctly. I also tried editing the src/Illuminate/Routing/Router.php bind() function to echo something but can't see it echo on production (does on local).

There must be some kind of class/file caching on my production box. Not sure if this is Laravel issue or something with my DigitialOcean box.


回答1:


This is probably due to Laravels pre-compiling.

The framework pre-compiles certain classes that are used on basically every request. This serves the purpose of performance optimization. Files to compile can be specified in config/compile.php under files. The default one looks like this:

'files' => [
    realpath(__DIR__.'/../app/Providers/AppServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/BusServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/ConfigServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/EventServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/RouteServiceProvider.php'),
],

When running php artisan optimize when debugging is not enabled (or with the --force option) Those listed files and other framework classes will be written to storage/framework/compiled.php. (in Laravel 5.0.16 the path has been changed to vendor/compiled.php)

Try running php artisan clear-compiled or php artisan optimize and your "new" RouteServiceProvider should be used.


Background info

php artisan optimize is called every time you run composer update composer install (and composer create-project) because it is registered as post script:

"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ]
},



回答2:


Try putting bind section before defining route.

$router->bind('active_notification_id', function ($id)
{
    echo 'here'; echo $id; exit;

    // code
});

$router->get('/notifications/{active_notification_id}/open', 'NotificationsController@open');


来源:https://stackoverflow.com/questions/28925057/route-model-binding-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!