【问题标题】:Laravel Route Model Binding - Laravel 5.7Laravel 路由模型绑定 - Laravel 5.7
【发布时间】:2021-05-24 23:34:24
【问题描述】:

我正在尝试使用 Laravel 的路由模型绑定。我在 RoutesServiceProvider 中设置了一个绑定来执行一些自定义解析逻辑。这适用于需要字符串名称和 id 来解析的 attributable 参数。

但是,当我尝试键入强制转换方法以利用另一个模型的隐式绑定时,它会失败并出现错误

传递给 Illuminate\Routing\Router::{closure}() 的参数 2 必须是 App\Models\Staff 的实例,给定字符串,在 /var/www/html/ngj_form/vendor/laravel/framework/ 中调用src/Illuminate/Routing/Route.php 在第 198 行

经过一些调试,我可以看到它在下面的方法定义中将路由的{attrId} 部分作为第二个类型转换参数传递。 ID 是一个字符串,因此它失败了。但是为什么它甚至试图传递这个参数呢?

路线如下所示:

Route::get('/admin/create-staff-payment/{attributable}/{attrId}/staff-member/{staff}/', 'Admin\StaffBalances@granularStaffBalance');

类型转换控制器方法如下所示:

 public function granularStaffBalance(Attributable $attributable, Staff $staff)
{
    dd('huh?');
}

RouteServiceProvider 看起来像这样:

  public function boot()
{

    // Bind Attributable (wedding|trial)
    Route::bind('attributable', function ($attributable, $route) {

        $attributableId = $route->parameter('attrId');

        switch($attributable){
            case 'wedding':
                $attributable = Wedding::class;
                break;
            case 'trial':
                $attributable = Trial::class;
                break;
            default:
                throw new \Exception('Type parameter provided is not supported.'); //TODO change this to 404 redirect
        }

        return $attributable::where('id', $attributableId)->firstOrFail();
    });

...

【问题讨论】:

    标签: php laravel route-model-binding


    【解决方案1】:

    好的,经过进一步研究(不感谢 Laravel 文档)我认为这是预期的行为。如果您在路由中传递三个参数,您的控制器方法将期望按照它们在 url 中出现的顺序接收它们。我想我从文档中假设参数的选择比这更智能,但我猜错了。

    我解决它的方法是通过另一个 post 的答案。

    显然你可以忘记参数作为丢弃它们的一种方式,这让我的控制器方法接受两个参数而不是三个。

    所以现在我的RouteServiceProvider 看起来像这样(在此处发布以防对其他人有帮助):

     public function boot()
        {
    
            parent::boot();
    
            // Bind Attributable (wedding|trial)
            Route::bind('attributable', function ($attributable, $route) {
    
                $attributableId = $route->parameter('attrId');
    
                switch($attributable){
                    case 'wedding':
                        $attributable = Wedding::class;
                        break;
                    case 'trial':
                        $attributable = Trial::class;
                        break;
                    default:
                        throw new \Exception('Type parameter provided is not supported.'); //TODO change this to 404 redirect
                }
    
                // We don't want to use this parameter in any controller methods so we can drop it here.
                // This fixes an issue where it was being passed into methods that needs $attributable, $staff
                $route->forgetParameter('attrId');
    
                return $attributable::where('id', $attributableId)->firstOrFail();
            });
    ...
    

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 2017-06-22
      • 1970-01-01
      • 2019-02-27
      • 2018-12-17
      • 2015-07-12
      • 2020-05-05
      • 2018-04-05
      • 2015-08-28
      相关资源
      最近更新 更多