撅屋墨规甲味焊顺痪厘沮垮到
前置操作是基于基类Controller来实现的:
所以必须继承Controller
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; //要使用前置操作,必须先继承基类 class Index extends \think\Controller { //继承基类中的前置方法配置列表 protected $beforeActionList=[]; //自定义属性 protected $siteName; public function demo1() { return $this->siteName; } public function demo2() { return $this->siteName; } public function demo3() { return $this->siteName; } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; //要使用前置操作,必须先继承基类 class Index extends \think\Controller { //继承基类中的前置方法配置列表 protected $beforeActionList=[ //为空,表明before()是当前类中全部操作的前置操作 //即demo1(),demo2(),demo3()执行之前都必须执行before() 'before1'=>'' ]; //自定义属性 protected $siteName; protected function before1() { $this->siteName="萌面人资料铺"; } public function demo1() { return $this->siteName; } public function demo2() { return $this->siteName; } public function demo3() { return $this->siteName; } }
执行:demo1(),demo2(),demo3()
通过请求对象来初始化siteName
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; //要使用前置操作,必须先继承基类 class Index extends \think\Controller { //继承基类中的前置方法配置列表 protected $beforeActionList=[ //为空,表明before()是当前类中全部操作的前置操作 //即demo1(),demo2(),demo3()执行之前都必须执行before() 'before1'=>'' ]; //自定义属性 protected $siteName; protected function before1() { //通过请求对象来初始化siteName //$this->request->param()是Controller中的方法,因为继承了Controller //所以可以直接使用 $this->siteName=$this->request->param('name'); } public function demo1() { return $this->siteName; } public function demo2() { return $this->siteName; } public function demo3() { return $this->siteName; } }
执行:
前置方法before2只允许demo2使用:
<?php namespace app\index\controller; //要使用前置操作,必须先继承基类 class Index extends \think\Controller { //继承基类中的前置方法配置列表 protected $beforeActionList=[ //为空,表明before()是当前类中全部操作的前置操作 //即demo1(),demo2(),demo3()执行之前都必须执行before() 'before1'=>'', //前置方法before2只允许demo2使用 'before2'=>['only'=>'demo2'] ]; //自定义属性 protected $siteName; protected function before1() { //通过请求对象来初始化siteName //$this->request->param()是Controller中的方法,因为继承了Controller //所以可以直接使用 $this->siteName=$this->request->param('name'); } protected function before2() { $this->siteName="ThinkPHP5从入门到精通"; } public function demo1() { return $this->siteName; } public function demo2() { return $this->siteName; } public function demo3() { return $this->siteName; } }
执行:
前置方法before3排除demo1,demo2,只对demo3有效:
<?php namespace app\index\controller; //要使用前置操作,必须先继承基类 class Index extends \think\Controller { //继承基类中的前置方法配置列表 protected $beforeActionList=[ //为空,表明before()是当前类中全部操作的前置操作 //即demo1(),demo2(),demo3()执行之前都必须执行before() 'before1'=>'', //前置方法before2只允许demo2使用 'before2'=>['only'=>'demo2'], //前置方法before3排除demo1,demo2,只对demo3有效 'before3'=>['except'=>'demo1,demo2'] ]; //自定义属性 protected $siteName; protected function before1() { //通过请求对象来初始化siteName //$this->request->param()是Controller中的方法,因为继承了Controller //所以可以直接使用 $this->siteName=$this->request->param('name'); } protected function before2() { $this->siteName="ThinkPHP5从入门到精通"; } protected function before3() { $this->siteName="一只穿云箭,千军万马来相见。"; } public function demo1() { return $this->siteName; } public function demo2() { return $this->siteName; } public function demo3() { return $this->siteName; } }
执行:
跺廊牟韧耪祥磁米温恳曹夕玻