Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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 路由上的Laravel策略:函数1的参数太少,预期传递2_Php_Laravel_Laravel 6_Policies - Fatal编程技术网

Php 路由上的Laravel策略:函数1的参数太少,预期传递2

Php 路由上的Laravel策略:函数1的参数太少,预期传递2,php,laravel,laravel-6,policies,Php,Laravel,Laravel 6,Policies,我创建了一个策略来保护我的模型。我想做的是阻止任何人编辑一个没有的思想记录 web.php Auth::routes(); Route::prefix('/')->middleware(['auth','can:viewAny,App\ThoughtRecord'])->group(function() { Route::get('/record/{id}/edit', 'ThoughtRecordController@edit')->middleware('can

我创建了一个策略来保护我的模型。我想做的是阻止任何人编辑一个没有的思想记录

web.php

Auth::routes();

Route::prefix('/')->middleware(['auth','can:viewAny,App\ThoughtRecord'])->group(function() {

    Route::get('/record/{id}/edit', 'ThoughtRecordController@edit')->middleware('can:isOwner,App\ThoughtRecord');

    Route::patch('/record/{thoughtRecord}','ThoughtRecordController@update')->middleware('can:isOwner,App\ThoughtRecord');

});
public function isOwner(User $user, ThoughtRecord $thoughtRecord)
{
    return $user->id == $thoughtRecord->user_id;

}
ThoughtRecordPolicy.php

Auth::routes();

Route::prefix('/')->middleware(['auth','can:viewAny,App\ThoughtRecord'])->group(function() {

    Route::get('/record/{id}/edit', 'ThoughtRecordController@edit')->middleware('can:isOwner,App\ThoughtRecord');

    Route::patch('/record/{thoughtRecord}','ThoughtRecordController@update')->middleware('can:isOwner,App\ThoughtRecord');

});
public function isOwner(User $user, ThoughtRecord $thoughtRecord)
{
    return $user->id == $thoughtRecord->user_id;

}
->中间件(['auth','can:viewAny,App\ThoughtRecord'])
工作得非常好。其他路由上的中间件没有通过
->中间件('can:isOwner,App\ThoughtRecord')
,并产生以下错误:

错误

Symfony\Component\Debug\Exception\FatalThroTableError 函数App\Policies\ThoughtRecordPolicy::isOwner()的参数太少,在第706行的/Applications/MAMP/htdocs/think records/vendor/laravel/framework/src/light/Auth/Access/Gate.php中传入了1个,预期正好是2个

编辑:

我将路线改为:

Route::get('/record/{thoughtRecord}/edit','ThoughtRecordController@edit“)->中间件('can:isOwner,thoughtRecord')


现在我得到了403分,条件是我非常肯定这是真的

您将第二个参数错误地传递给了
isoowner
策略的方法

以下方面应起作用:

Route::get('/record/{thoughtRecord}/edit','ThoughtRecordController@edit')
->中间件(“can:isOwner,thoughtRecord”);
根据:

在本例中,我们将向can中间件传递两个参数。第一个是我们希望授权的操作的名称,第二个是我们希望传递给策略方法的路由参数。在本例中,由于我们使用隐式模型绑定,因此Post模型将传递给策略方法

因此,您基本上需要使用隐式模型绑定,并将route参数作为第二个参数传入


希望有帮助

谢谢。奇怪的是,我刚试过。我现在遇到的问题是403,条件是正确的。理想情况下不应该。这可能是因为另一个中间件
can:viewAny
。试着先把它去掉,看看问题出在哪里。尝试从
isOwner
返回
true
,以便调试问题所在。啊,等等。我刚刚发现问题出在控制器上的@edit方法上,它将
$id
作为参数传递。不
ThoughtRecord$ThoughtRecord
。这管用!