Yii2 在模块内映射 Controller 到其它控制器

上文讲解关于如何映射 Action,本文讲解如何映射 Controller。

0x00 实现

首先,在你的 Module 里,定义一个 $controllerRedirectMap 变量。

public $controllerRedirectMap = [];

重写 createControllerByID 方法。

public function createControllerByID($id) { if(isset($this->controllerRedirectMap[$id])) { $id = $this->controllerRedirectMap[$id]; } return parent::createControllerByID($id); }

0x01 用法

public $controllerRedirectMap = [ 'foo' => 'bar' // 访问 `module/foo/index` 将会被重定向至 `module/bar/index` 'foo/bar' => 'bar/foo' // 访问 `module/foo/bar/index` 将会被重定向至 `module/bar/foo/index` ];
本作品采用《CC 协议》,转载必须注明作者和本文链接
Former WinForm and PHP engineer. Now prefer Golang and Rust, and mainly working on DevSecOps and Kubernetes.
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

未填写
文章
67
粉丝
591
喜欢
1236
收藏
1136
排名:13
访问:32.4 万
私信
所有博文
社区赞助商