【问题标题】:laravel 4 - route with two bound modelslaravel 4 - 具有两个绑定模型的路线
【发布时间】:2014-02-18 10:25:07
【问题描述】:

我需要检查{subcategory} 是否有父类别{category}。如何在第二次绑定中获得{category} 的模型?

我试过$route->getParameter('category');。 Laravel 抛出 FatalErrorException 并显示消息“已达到 '100' 的最大函数嵌套级别,正在中止!”。

Route::bind('category', function ($value) {
    $category = Category::where('title', $value)->first();
    if (!$category || $category->parent_id) {
        App::abort(404);
    }
    return $category;
});

Route::bind('subcategory', function ($value, $route) {
    if ($value) {
        $category = Category::where('title', $value)->first();
        if ($category) {
            return $category;
        }
        App::abort(404);
    }
});

Route::get('{category}/{subcategory?}', 'CategoriesController@get');

更新:

现在我做了这个,但我认为这不是最好的解决方案。

Route::bind('category', function ($value) {
    $category = Category::where('title', $value)->whereNull('parent_id')->first();
    if (!$category) {
        App::abort(404);
    }

    Route::bind('subcategory', function ($value, $route) use ($category) {
        if ($value) {
            $subcategory = Category::where('title', $value)->where('parent_id', $category->id)->first();
            if (!$subcategory) {
                App::abort(404);
            }

            return $subcategory;
        }
    });

    return $category;
});

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    你可以试试这个,应该可以工作,代码是不言自明的(询问是否需要解释):

    Route::bind('category', function ($value) {
        $category = Category::where('title', $value)->first();
        if (!$category || $category->parent_id) App::abort(404);
        return $category;
    });
    
    Route::bind('subcategory', function ($value, $route) {
        $category = $route->parameter('category');
        $subcategory = Category::where('title', $value)->whereParentId($category->id);
        return $subcategory->first() ?: App::abort(404); // shortcut of if
    });
    
    Route::get('{category}/{subcategory?}', 'CategoriesController@get');
    

    更新:(正如OP 声称的那样,Route 类中没有可用的parameter 方法):

    /**
     * Get a given parameter from the route.
     *
     * @param  string  $name
     * @param  mixed  $default
     * @return string
     */
    public function getParameter($name, $default = null)
    {
        return $this->parameter($name, $default);
    }
    
    /**
     * Get a given parameter from the route.
     *
     * @param  string  $name
     * @param  mixed  $default
     * @return string
     */
    public function parameter($name, $default = null)
    {
        return array_get($this->parameters(), $name) ?: $default;
    }
    

    【讨论】:

    • 在 Laravel 4 Route 类中没有 parameter() 方法。
    • @DmitryGres,Route 类中有一个parameter 方法,请确保您检查了正确的class
    • 没有paremeter 方法(laravel.com/api/class-Illuminate.Routing.Route.html)。当我尝试 laravel 抛出 FatalErrorException 并显示消息“调用未定义的方法 Illuminate\Routing\Route::parameter()”。
    • @DmitryGres, Check this, Laravel 4.1 如果您使用的是旧版本,您也可以使用getParameter 方法。
    • 我试过$route->getParameter('category');。 Laravel 抛出 FatalErrorException 并显示消息“已达到 '100' 的最大函数嵌套级别,正在中止!”。
    【解决方案2】:

    我现在不能为你测试这个,但是闭包函数接收一个 $value 和 $route。 最后一个是 '\Illuminate\Routing\Route' (http://laravel.com/api/class-Illuminate.Routing.Route.html) 的一个实例,也许你可以使用getParameter() 方法来检索一些数据......

    【讨论】:

    • 我试过$route->getParameter('category');。 Laravel 抛出 FatalErrorException 并显示消息“已达到 '100' 的最大函数嵌套级别,正在中止!”。
    • 试试resolveParameter('category'),如果不行,我就放弃了。
    【解决方案3】:

    我最近在尝试自动验证我的故事是否存在于我的会话模型中时遇到了同样的问题。因此,我尝试使用模型绑定检查我的 Session 模型中我的 Story 模型是否存在。

    这是我的解决方案

    $router->bind('stories', function($value, $route) {
            $routeParameters = $route->parameters();
            $story = Story::where('id', $value)->where('poker_session_id',    $routeParameters['poker_sessions']->id)->first();
    
            if (!$story) {
                throw new NotFoundHttpException;
            }
    
            return $story;
        });
    

    您实际上可以使用 $route->parameters() 获取路由参数,它返回一个数组。就我而言,“poker_sessions”键包含我想要的 PokerSession 模型。

    请小心,仅当您有 /category/{category}/subcategory/{subcategory} 之类的 url 时才使用它。不是没有任何 {category} 的子类别。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2013-10-27
      • 2021-01-05
      • 2018-01-08
      • 2021-05-24
      • 2016-06-04
      • 2021-08-13
      • 1970-01-01
      • 2013-07-13
      • 2018-12-17
      相关资源
      最近更新 更多