Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 隐式路由模型绑定_Php_Laravel_Laravel 5_Laravel Routing_Laravel 5.6 - Fatal编程技术网

Php 隐式路由模型绑定

Php 隐式路由模型绑定,php,laravel,laravel-5,laravel-routing,laravel-5.6,Php,Laravel,Laravel 5,Laravel Routing,Laravel 5.6,Laravel的隐式路由模型绑定不起作用。它不查找标识符指示的记录。我要买一个全新的模型 鉴于此代码: Route::get('users/{user}', function (App\User $user, $id) { $user2 = $user->find($id); return [ [get_class($user), $user->exists, $user], [get_class($user2), $user2->

Laravel的隐式路由模型绑定不起作用。它不查找标识符指示的记录。我要买一个全新的模型

鉴于此代码:

Route::get('users/{user}', function (App\User $user, $id) {
    $user2 = $user->find($id);
    return [
        [get_class($user), $user->exists, $user],
        [get_class($user2), $user2->exists],
    ];
});
这个URL:
/users/1

我得到这个输出:

[["App\\User",false,[]],["App\\User",true]]
我使用的是PHP7.2和Laravel5.6


其他信息

我在其他Laravel项目中成功地完成了隐式路由模型绑定。我正在处理一个现有的代码库。据我所知,这项功能以前从未使用过

用户记录已存在。它没有被软删除。该模型不使用
SoftDeletes
特性

我尝试过使用各种独特的路由名称和其他模型


我已经检查了
App\Http\Kernel
类中常见的罪魁祸首
$middlewareGroups
\Lightlight\Routing\Middleware\SubstituteBindings::class,
web
部分,并且
$routeMiddleware
包含
'bindings'=>\Lightlight\Routing\Middleware\SubstituteBindings::class,
在Laravel中应该可以正常工作。我刚刚在我的Laravel 5.6应用程序中验证了这一点,没有问题

可能的情况为什么会出现这种情况:

  • 用户被软删除
  • 此路由不在
    web.php
    api.php
    文件中-这两个组都在
    app/Http/Kernel.php
    文件中的
    $middlewaregroups
    属性中设置了
    绑定
    (或
    \lightlight\Routing\Middleware\SubstituteBindings::class
  • 您从其中一个组中删除了上述绑定
  • 您已经设置了一些自定义绑定。例如,如果您定义了如下代码:
    
    路由::绑定('user',函数($user){
    返回new\App\User();
    });
    

    然后,您将得到显示的结果,因为您使用自定义逻辑,只返回空的用户模型


如果您认为以上所有内容都是错误的,我将从全新的Laravel 5.6应用程序开始尝试复制该问题。

它在Laravel中应该可以正常工作。我刚刚在我的Laravel 5.6应用程序中验证了这一点,没有问题

可能的情况为什么会出现这种情况:

  • 用户被软删除
  • 此路由不在
    web.php
    api.php
    文件中-这两个组都在
    app/Http/Kernel.php
    文件中的
    $middlewaregroups
    属性中设置了
    绑定
    (或
    \lightlight\Routing\Middleware\SubstituteBindings::class
  • 您从其中一个组中删除了上述绑定
  • 您已经设置了一些自定义绑定。例如,如果您定义了如下代码:
    
    路由::绑定('user',函数($user){
    返回new\App\User();
    });
    

    然后,您将得到显示的结果,因为您使用自定义逻辑,只返回空的用户模型


如果您认为以上所有内容都是错误的,我将从全新的Laravel 5.6应用程序开始尝试复制此问题。

我最终解决了此问题。
routes/web.php
中的路由没有
web
中间件。这通常在
app/Providers/RouteServiceProvider.php中的
mapWebRoutes()
函数中完成。在某个时刻,在Laravel升级过程中,路由定义被破坏。看起来是这样的:

        Route::group([
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }
它可以使用旧的定义样式进行更新,如下所示:

        Route::group([
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }
相反,我只是从项目中复制了,所以现在看起来是这样的:

        Route::group([
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

我终于解决了这个问题。
routes/web.php
中的路由没有
web
中间件。这通常在
app/Providers/RouteServiceProvider.php中的
mapWebRoutes()
函数中完成。在某个时刻,在Laravel升级过程中,路由定义被破坏。看起来是这样的:

        Route::group([
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }
它可以使用旧的定义样式进行更新,如下所示:

        Route::group([
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }
相反,我只是从项目中复制了,所以现在看起来是这样的:

        Route::group([
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

是否对模型进行了任何自定义?列别名、行为等。用户实际存在。我从一个资源控制器和一个完全不同的模型开始了这段旅程。你在上面看到的是我试图提供的信息。它告诉我Laravel中有一些配置不正确。听起来好像
SubstituteBindings
没有运行。是否对模型进行了任何自定义?列别名、行为等。用户实际存在。我从一个资源控制器和一个完全不同的模型开始了这段旅程。你在上面看到的是我试图提供的信息。它告诉我Laravel中有一些配置不正确。听起来好像
SubstituteBindings
没有运行。我已经更新了我的问题以解决您的问题,但我可能遗漏了一些内容。@Sonny也许您应该试着验证真正运行的查询是什么?您可以尝试使用我的包,或者查看查询是否真的在运行,以及它的外观。您是否100%确定您正在使用与检查数据相同的数据库?我认为查询没有运行。中间件必须以某种方式短路。我完全迷路了。我再次更新了我的帖子,以显示该记录很容易加载。@Sonny对于您提供的信息,很难说更多。正如我所说的,您应该尝试使用一些数据库记录器来查看是否加载了查询。也许查询中添加了更多内容,也许您有一些自定义绑定,如果不查看整个应用程序代码,很难说更多内容。我已经更新了我的问题以解决您的问题,但我可能遗漏了一些内容。@Sonny也许您应该试着验证真正运行的查询是什么?您可以尝试使用我的包,或者查看查询是否真的在运行,以及它的外观。您是否100%确定您正在使用与检查数据相同的数据库?我认为查询没有运行。这个