在任何的框架开发中,都可能会用到一些重写的方法,目的是为了使框架更加灵活易用和解决composer包中已经写死的问题,我们如果直接在composer包中对内容进行修改,那么将会在composer包更新时,修改的内容将会失效,影响项目的正常运行,以下是Yii2框架中重写的方法:
官网部分的介绍为:
http://www.yiiframework.com/doc-2.0/guide-helper-overview.html#customizing-helper-classes
下面是代码步骤:
1.原来的类的内容为:
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\filters\auth; use Yii; use yii\base\Action; use yii\base\ActionFilter; use yii\helpers\StringHelper; use yii\web\Request; use yii\web\Response; use yii\web\UnauthorizedHttpException; use yii\web\User; /** * AuthMethod is a base class implementing the [[AuthInterface]] interface. * * @author Qiang Xue <qiang.xue@gmail.com> * @since 2.0 */ abstract class AuthMethod extends ActionFilter implements AuthInterface { /** * @var User the user object representing the user authentication status. If not set, the `user` application component will be used. */ public $user; /** * @var Request the current request. If not set, the `request` application component will be used. */ public $request; /** * @var Response the response to be sent. If not set, the `response` application component will be used. */ public $response; /** * @var array list of action IDs that this filter will be applied to, but auth failure will not lead to error. * It may be used for actions, that are allowed for public, but return some additional data for authenticated users. * Defaults to empty, meaning authentication is not optional for any action. * Since version 2.0.10 action IDs can be specified as wildcards, e.g. `site/*`. * @see isOptional() * @since 2.0.7 */ public $optional = []; /** * {@inheritdoc} */ public function beforeAction($action) { $response = $this->response ?: Yii::$app->getResponse(); try { $identity = $this->authenticate( $this->user ?: Yii::$app->getUser(), $this->request ?: Yii::$app->getRequest(), $response ); } catch (UnauthorizedHttpException $e) { if ($this->isOptional($action)) { return true; } throw $e; } if ($identity !== null || $this->isOptional($action)) { return true; } $this->challenge($response); $this->handleFailure($response); return false; } /** * {@inheritdoc} */ public function challenge($response) { } /** * {@inheritdoc} */ public function handleFailure($response) { throw new UnauthorizedHttpException('Your request was made with invalid credentials.'); } /** * Checks, whether authentication is optional for the given action. * * @param Action $action action to be checked. * @return bool whether authentication is optional or not. * @see optional * @since 2.0.7 */ protected function isOptional($action) { $id = $this->getActionId($action); foreach ($this->optional as $pattern) { if (StringHelper::matchWildcard($pattern, $id)) { return true; } } return false; } }
比如我们需要重写这个类中的异常处理方法:handleFailure($response),将英文改为中文的,内容如下:
/** * {@inheritdoc} */ public function handleFailure($response) { throw new UnauthorizedHttpException('令牌已失效,请重新登录'); }
2.那么我们写一个新类: 文件路径为:/common/rewrite/yii_filters_auth_AuthMethod.php ,我想让controller调用的类为下面的类
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\filters\auth; use Yii; use yii\base\Action; use yii\base\ActionFilter; use yii\helpers\StringHelper; use yii\web\Request; use yii\web\Response; use yii\web\UnauthorizedHttpException; use yii\web\User; /** * AuthMethod is a base class implementing the [[AuthInterface]] interface. * * @author Qiang Xue <qiang.xue@gmail.com> * @since 2.0 */ abstract class AuthMethod extends ActionFilter implements AuthInterface { /** * @var User the user object representing the user authentication status. If not set, the `user` application component will be used. */ public $user; /** * @var Request the current request. If not set, the `request` application component will be used. */ public $request; /** * @var Response the response to be sent. If not set, the `response` application component will be used. */ public $response; /** * @var array list of action IDs that this filter will be applied to, but auth failure will not lead to error. * It may be used for actions, that are allowed for public, but return some additional data for authenticated users. * Defaults to empty, meaning authentication is not optional for any action. * Since version 2.0.10 action IDs can be specified as wildcards, e.g. `site/*`. * @see isOptional() * @since 2.0.7 */ public $optional = []; /** * {@inheritdoc} */ public function beforeAction($action) { $response = $this->response ?: Yii::$app->getResponse(); try { $identity = $this->authenticate( $this->user ?: Yii::$app->getUser(), $this->request ?: Yii::$app->getRequest(), $response ); } catch (UnauthorizedHttpException $e) { if ($this->isOptional($action)) { return true; } throw $e; } if ($identity !== null || $this->isOptional($action)) { return true; } $this->challenge($response); $this->handleFailure($response); return false; } /** * {@inheritdoc} */ public function challenge($response) { } /** * {@inheritdoc} */ public function handleFailure($response) { throw new UnauthorizedHttpException('令牌已失效,请重新登录'); } /** * Checks, whether authentication is optional for the given action. * * @param Action $action action to be checked. * @return bool whether authentication is optional or not. * @see optional * @since 2.0.7 */ protected function isOptional($action) { $id = $this->getActionId($action); foreach ($this->optional as $pattern) { if (StringHelper::matchWildcard($pattern, $id)) { return true; } } return false; } }
注意:namespace和上面的那个AuthMethod类的要一样,不需要做任何修改,而不是按照 psr自动加载规则来命名的,这样会报错的。
我添加Yii::classMap 数组的值的新的My类的文件路径
Yii::$classMap['yii\filters\auth\AuthMethod'] = ['@common/rewrite/yii_filters_auth_AuthMethod.php'];
然后调用后,发现调用的是新的My类。
需要注意的是,新的类的名字必须和之前的类的名字一样,否则会出错,另外,namespace要一致,一样。
我们希望通过配置文件的方式,这样比较方面,我们可以这样做。
5.1 在/common/config下面添加文件 yii-class-map.php ,内容如下:
<?php return [ 'yii\filters\auth\AuthMethod' => '@common/rewrite/yii_filters_auth_AuthMethod.php', ];
在web/index.php的代码
$application = new yii\web\Application($config);
上面添加代码:
/** * yii class Map Custom * */ $yiiClassMap = require(__DIR__ . '/../config/YiiClassMap.php');if(is_array($yiiClassMap) && !empty($yiiClassMap)){ foreach($yiiClassMap as $namespace => $filePath){ Yii::$classMap[$namespace] = $filePath; } }
如果有多个项目入口,比如home、backend等,那么同样也按上面的方法中添加一下即可
这样,通过上面的配置文件,就可以把classMap执行了,以后如果添加classMap,直接在文件
/common/config/yii-class-map.php 文件里面的数组中添加一条数据就可以了。
无论从事什么行业,只要做好两件事就够了,一个是你的专业、一个是你的人品,专业决定了你的存在,人品决定了你的人脉,剩下的就是坚持,用善良專業和真诚赢取更多的信任。