relaxsd/laravel-model-gates 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

relaxsd/laravel-model-gates

Composer 安装命令:

composer require relaxsd/laravel-model-gates

包简介

Model gates for Laravel

README 文档

README

This packages allows authorized access to model attributes, so by using the Laravel Gates and Policies, you can reduce the data returned based on authorization.

Install

Pull this package in through Composer.

{
    "require": {
        "relaxsd/laravel-model-gates": "0.1.*"
    }
}

Usage

Here's an example of a model gate.

use Relaxsd\Gate\ModelGate;

class ProjectGate extends ModelGate {

    public function members()
    {
        if (\Gate::allows('indexForProject', [\App\Member::class, $this->entity])) {
            return $this->entity->members;
        }
        return new \Illuminate\Database\Eloquent\Collection(); // Not authorized: return an empty collection instead. 
    }

}

Next, on your entity, pull in the Relaxsd\Gate\ModelGateTrait trait, which will automatically instantiate your model gate class.

Here's an example - maybe a Laravel User model.

<?php

use Relaxsd\Gate\ModelGateTrait;

class Project extends \Eloquent {

    use ModelGateTrait;

    protected $gate = 'ProjectGate';

}

That's it! You're done. Now, within your code, you can do:

    return $project->gate()->members; // Only the members we're allowed to see

Notice how the call to the gate() method (which will return your new or cached model gate object) also provides the benefit of making it perfectly clear where you must go, should you need to modify the implementation.

Have fun!

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固