路由模型绑定未触发

Route Model Binding Not Firing

我有一些 route/model 绑定设置。其中大约有十个用于各种 ID。没什么特别的:

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

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

    // code
});

绑定根本没有触发。在其他八个中工作正常,但对于其中两个它只是不开火。它直接进入带有空模型的控制器,这会导致我的代码崩溃。

更疯狂的是,它们在我的本地机器上都能正常工作 (Windows),但只有在服务器 (Ubuntu) 上才会出现这个问题。我的 php 版本只差一个次要版本。但是有 8 个绑定有效,只是那两个根本不会触发。

有人有想法吗?

更新:所以实际上 none 我的路线似乎会在生产中回显。我 "assumed" 其他人正在工作,因为他们工作正常。我还尝试编辑 src/Illuminate/Routing/Router.php bind() 函数来回显某些内容,但在生产环境中看不到它的回显(在本地)。

我的生产箱上一定有某种 class/file 缓存。不确定这是 Laravel 问题还是我的 DigitialOcean 盒子的问题。

尝试在定义路由之前放置绑定部分。

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

    // code
});

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

这可能是 Laravels 预编译的原因。

框架预编译 某些类 基本上每个请求都会用到。这服务于性能优化的目的。要编译的文件可以在 files 下的 config/compile.php 中指定。 default one 看起来像这样:

'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'),
],

当 运行ning php artisan optimize 未启用调试时(或使用 --force 选项)那些列出的文件和其他框架 类 将被写入 storage/framework/compiled.php。 (在 Laravel 5.0.16 中,路径已更改为 vendor/compiled.php

试试 运行ning php artisan clear-compiledphp artisan optimize,应该使用你的“新”RouteServiceProvider


背景资料

php artisan optimize 每次 运行 composer update composer install(和 composer create-project)都会被调用,因为它被注册为 post 脚本:

"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"
    ]
},