你好,当我将一个变量传递给中间件时,我收到了 403 这是我的构造函数:
public function __construct(XristesRepository $xristesRepo)
{
$table = Table::where('model_name', 'Xristes')->first();
$this->xristesRepository = $xristesRepo;
$this->permissions = Auth::user()->getPermissions(Auth::user()->id, $table->id);
$perms = [];
if (in_array('create', $this->permissions)) {
$perms[] = '\'create\', \'store\' ';
};
if (in_array('edit', $this->permissions)) {
$perms[] = '\'edit\', \'update\' ';
};
if (in_array('delete', $this->permissions)) {
$perms[] = '\'destroy\'';
};
$perms = '\'index\', ' .implode(',', $perms);
$this->middleware('userPermissions', ['except' => [$perms]]);
}
perms 变量的输出是:
'index', 'create', 'store', 'edit', 'update','destroy'
当我将上面的输出粘贴到中间件时,如:
$this->middleware('userPermissions', ['except' => ['index', 'create', 'store', 'edit', 'update','destroy']]);
一切正常,任何人都可以帮助我,请问问题出在哪里,因为我得到了相同的结果,但是当我使用变量时,当我粘贴输出时,它工作正常
最佳答案
您的 $perms
数组是错误的。
在这里,您要将创建和存储添加为字符串:
if (in_array('create', $this->permissions)) {
$perms[] = '\'create\', \'store\' ';
};
然后您将 edit
和 update
作为数组的第二个元素添加。
更好的选择是:
$perms = ['index'];
if (in_array('create', $this->permissions)) {
array_push($perms, 'create', 'store');
};
if (in_array('edit', $this->permissions)) {
array_push($perms, 'edit', 'update');
};
if (in_array('delete', $this->permissions)) {
array_push($perms, 'destroy');
};
关于php - 当我内爆异常(exception)页面时 Laravel 中间件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43542687/