php - 当我内爆异常(exception)页面时 Laravel 中间件错误

标签 php mysql laravel

你好,当我将一个变量传递给中间件时,我收到了 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\' ';
};

然后您将 editupdate 作为数组的第二个元素添加。

更好的选择是:

$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/

相关文章:

php - Laravel 5.2 条件验证

PHP:即使有多个具有相同名称的复选框,也将提交的数据保留在表单中

mysql - 相关键/值表索引/搜索

laravel - 无法从 dist : The "https://api. github.com/repos/laravel/spark-aurelius/zipball 下载 laravel/spark-aurelius

php - 使用 xampp 的 PHP 应用程序的权限被拒绝

php - php中@字符的含义

php - header() 不会自动重定向到另一个索引页

javascript - 由 window.location.href 调用时 SQL 查询不起作用

php - mysql_fetch_assoc() 的结果将旧值复合到新值上。有任何想法吗?

php - Laravel Passport 在 Laravel 5.3 中如何工作?