首页 > 其他分享 >Yii2 过滤器

Yii2 过滤器

时间:2024-03-30 14:44:24浏览次数:15  
标签:actions Access Allow 过滤器 POST Yii2 true

Yii2 过滤器

过滤器AccessController

AccessControl 基于一组规则提供简单的访问控制

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'only'=>['index'],
                'rules' => [
                    //任何人都可以访问index
                    [
                        'actions'=>['index'],
                        'allow'=>true,
                        'roles'=>['?']
                    ],
                    //指定IP游客可以访问view
                    [
                        'actions'=>['view'],
                        'allow'=>true,
                        'ips'=>[
                            '192.168.*',
                            '10.13.1.182'
                        ],
                        'roles'=>['?']
                    ],
                    //登录用户可以访问update
                    [
                        'actions'=>['update'],
                        'allow'=>true,
                        'roles'=>['@']
                    ],
                    //登录用户需要post访问update
                    [
                        'actions'=>['update'],
                        'allow'=>true,
                        'roles'=>['@'],
                        'verbs'=>['post']
                    ],
                    //指定日期可以访问test
                    [
                        'actions'=>['test'],
                        'allow' => true,
                        'matchCallback'=>function($rule,$action){
                            return date('d-m') === '11-11';
                        }
                    ],
                ]
            ],
        ];
    }

参数说明

actions: 设置哪个动作匹配此规则。
roles: 设定哪个角色匹配此规则。
    *: 任何用户,包括匿名和验证通过的用户。
     ?: 匿名用户。
     @: 验证通过的用户。
ips: 设定哪个客户端IP匹配此规则。
verbs: 设定哪种请求类型(例如:GET, POST)匹配此规则。
matchCallback:指定一个PHP回调,以确定应用该规则。
denyCallback:PHP回调,当规则禁止访问的时候会被调用。

过滤器VerbFilter

VerbFilter 是一个操作过滤器它通过 HTTP 请求方法进行过滤

public function behaviors()
{
    return [
        'verbs' => [
        'class' => \yii\filters\VerbFilter::className(),
        'actions' => [
            'index'  => ['GET'],
            'view'   => ['GET'],
            'create' => ['GET', 'POST'],
            'update' => ['GET', 'PUT', 'POST'],
            'delete' => ['POST', 'DELETE'],
            ],
        ],
    ];
}

过滤器 AjaxFilter

AjaxFilter 只允许限制 Ajax 请求的访问

public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\AjaxFilter',
                'only' => ['index']
            ],
        ];
    }

过滤器 corsFilter

允许开发人员授予对第三方代码的访问权限(来自外部域的 Ajax 调用)

public function behaviors()
    {
        return [
            'corsFilter' => [
                'class' => \yii\filters\Cors::className(),
                'cors' => [
                    // restrict access to
                    'Origin' => ['http://www.myserver.com', 'https://www.myserver.com'],
                    // Allow only POST and PUT methods
                    'Access-Control-Request-Method' => ['POST', 'PUT'],
                    // Allow only headers 'X-Wsse'
                    'Access-Control-Request-Headers' => ['X-Wsse'],
                    // Allow credentials (cookies, authorization headers, etc.) to be exposed to the browser
                    'Access-Control-Allow-Credentials' => true,
                    // Allow OPTIONS caching
                    'Access-Control-Max-Age' => 3600,
                    // Allow the X-Pagination-Current-Page header to be exposed to the browser.
                    'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
                ],

            ],
        ];
    }

标签:actions,Access,Allow,过滤器,POST,Yii2,true
From: https://www.cnblogs.com/hu308830232/p/18105471

相关文章

  • yii2数据库访问对象
    yii2数据库访问对象配置数据库链接$db=newyii\db\Connection(['dsn'=>'mysql:host=localhost;dbname=xhj','username'=>'root','password'=>'123456',......
  • yii2 扩展
    yii2扩展示例安装扩展composerrequire--prefer-distyiisoft/yii2-imagine使用扩展useyii\imagine\Image;publicfunctionactionIndex(){Image::thumbnail('@webroot/img/test.jpg',120,120)->save(Yii::getAlias('@runt......
  • yii2表单使用
    yii2表单使用模型models/form/EntryForm.php<?phpnamespaceapp\models\form;useyii\base\Model;classEntryFormextendsModel{public$name;public$email;publicfunctionrules(){return[[['name',&......
  • yii2 rules验证规则大全
    yii2rules验证规则大全required : 必须值验证属性[['name','email'],'required'][['name'],'required','message'=>'提示信息']email : 邮箱验证[['email'],'email','message'=......
  • Yii2行为用法
    Yii2行为用法使用行为(behavior)可以在不修改现有类的情况下,对类的功能进行扩充行为类(app\common\behaviors\MyBehavior)<?phpnamespaceapp\common\behaviors;useyii\base\Behavior;classMyBehaviorextendsBehavior{public$name;public$age;......
  • yii2模块
    yii2模块模块是独立的软件单元,由模型, 视图, 控制器和其他支持组件组成, 终端用户可以访问在应用主体中已安装的模块的控制器, 模块被当成小应用主体来看待,和应用主体不同的是, 模块不能单独部署,必须属于某个应用主体。模块文件结构modulesadmincontrollers......
  • yii2服务定位器
    yii2服务定位器服务定位器是注册和访问组件的对象注册组件useyii\di\ServiceLocator;useyii\caching\FileCache;$locator=newServiceLocator;//通过一个可用于创建该组件的类名,注册"cache"(缓存)组件。$locator->set('cache','yii\caching\ApcCache');//通过......
  • yii2响应(Responses)
    yii2响应(Responses)状态码Yii::$app->response->statusCode=200;异常yii\web\BadRequestHttpException:statuscode400.yii\web\ConflictHttpException:statuscode409.yii\web\ForbiddenHttpException:statuscode403.yii\web\GoneHttpException:......
  • yii2过滤器
    yii2过滤器过滤器是控制器动作执行之前或之后执行的对象。示例过滤器common\components\ActionTimeFilter<?phpnamespaceapp\common\components;useYii;useyii\base\ActionFilter;classActionTimeFilterextendsActionFilter{private$_startTime;......
  • yii2 资源
    yii2资源AppAsset.php<?phpnamespaceapp\assets;useyii\web\AssetBundle;classAppAssetextendsAssetBundle{public$basePath='@webroot';public$baseUrl='@web';public$css=['css/site.css�......