PHP code example of qimao / authentication

1. Go to this page and download the library: Download qimao/authentication library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

qimao / authentication example snippets


   'language' => 'zh-CN',
   'components' => [
       	//...其他配置
           'i18n' => [
               'translations' => [
                   '*' => [
                       'class'    => 'yii\i18n\PhpMessageSource',
                       'basePath' => '@app/messages',
                   ],
               ],
           ],
           'urlManager' => [
               'enablePrettyUrl' => true,
               //'enableStrictParsing' => true,
               'showScriptName' => false,
               'rules' => [
                   'GET <controller:(admin)>/<action:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<controller:[\w-]+>/<action:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>/<action1:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>/<action1:[\w-]+>/<action2:[\w-]+>' => 'site/index',
               ],
           ],
       ],
   

   Yii::setAlias('@static', dirname(__DIR__) . '/web/static' );
   

      //实例代码,uid为当前用户id,noAuthUser为无需校验的用户id集合
   	public function beforeAction($action)
       {
           if (parent::beforeAction($action)){
               if (!in_array($uid,$this->noAuthUser)){
                   $route = '/'.$action->controller->id.'/'.$action->id;
                   $code = $this->authenticate($route,$uid);
                   if ($code){
                       $this->verifyReponse([], Yii::t('authentication/auth', 'authentication_fail_'.$code));
                       return false;
                   }
               }
               return true;
           }else{
               return false;
           }
       }
   

   	//页面路由控制实例,uid为当前用户id,noAuthUser为无需校验的用户id集合
   	public function actionIndex()
       {
           if (!in_array($uid,$this->noAuthUser)){
               $route = '/'. Yii::$app->request->pathInfo;
               $code = $this->authenticate($route,$uid);
               if ($code){
                   return $this->verifyReponse([], Yii::t('authentication/auth', 'authentication_fail_'.$code));
               }
           }
           return $this->render('index');
       }
   
   
   	//配置文件定向实例
           'urlManager' => [
               'enablePrettyUrl' => true,
               //'enableStrictParsing' => true,
               'showScriptName' => false,
               'rules' => [
                   'GET <controller:(admin)>/<action:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<controller:[\w-]+>/<action:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>/<action1:[\w-]+>' => 'site/index',
                   'GET <modules:(admin)>/<module:[\w-]+>/<controller:[\w-]+>/<action:[\w-]+>/<action1:[\w-]+>/<action2:[\w-]+>' => 'site/index',
               ],
           ],
   

/authentication
	/common
		AuthenticationConstant.php
		funcionts.php
		InputValidator.php
	/controllers
		AuthController.php
		AuthRoleController.php
		BaseController.php
		SiteController.php
	/logic
		AbstractBaseLogic.php
		Auth.php
		AuthRole.php
	/messages
	/models
		Auth.php
		AuthRole.php
		AuthRoleTable.php
		AuthTable.php
		BaseModel.php
		RoleAuthRelation.php
		RoleAuthRelationTable.php
		UserAuthRole.php
		UserAuthRoleTable.php
	/traits
		Auth.php
		BaseController.php
	sql