yii的控制器和视图的命名区分yii2 url大小写写么

Yii2 获取模块名、控制器名、方法名
在视图中:
$this-&context-&module-&id
控制器名 $this-&context-&id
方法名 $this-&context-&action-&id
Yii::$app-&request-&hostInfo
在控制器中
Yii::$app-&controller-&module-&
Yii::$app-&controller-&id
Yii::$app-&controller-&action-&
模块名 $this-&module-&
控制器名 $this-&
$this-&action-&
在控制器的 beforeAction 方法中(方法接收$action参数)
$action-&controller-&module-&
控制器名 $action-&controller-&
======================================
[Url::current] – 现在测试本地路径(http://daxia./mobile/hmConnections/user/verify-user)
a: //获取当前路径 – 相对路径
$url = Url::current();
举例:/mobile/hmConnections/user/verify-user
b: //获取当前路径 – 相对路径
$url = Url::current([‘id’ =& 1], false);
例如: /mobile/hmConnections/user/verify-user?id=1
c: //获取当前路径 – 绝对路径
$url = Url::current([‘id’ =& 1], true);
例如: http://daxia./mobile/hmConnections/user/verify-user?id=1
d: //获取当前路径 – 绝对路径 传输协议-http
$url = Url::current([‘id’ =& 1], ‘http’);
例如: http://daxia./mobile/hmConnections/user/verify-user?id=1
e: //获取当前路径 – 绝对路径 传输协议-https
$url = Url::current([‘id’ =& 1], ‘https’);
例如: https://daxia./mobile/hmConnections/user/verify-user?id=1
[Url::toRoute] – 获取某一地址 =& 现在测试本地路径(http://daxia./mobile/hmConnections/user/verify-user)
1://获取某地址 – 相对路径
$url = Url::toRoute(‘site/index’);
例如: /mobile/hmConnections/site/index
2://获取某地址 – 相对路径
$url = Url::toRoute(‘site/index’, false);
例如: /mobile/hmConnections/site/index
说明: 等价于1 因为默认是false
3://获取某地址 – 相对路径
$url = Url::toRoute([‘site/index’, ‘id’ =& 1]);
例如: /mobile/hmConnections/site/index?id=1
4://获取某地址的 – 绝对路径
$url = Url::toRoute(‘site/index’, true);
例如: http://daxia./mobile/hmConnections/site/index
5://获取某地址的 – 绝对路径
$url = Url::toRoute(‘site/index’, [‘id’ =& 1]);
例如: http://daxia./mobile/hmConnections/site/index
说明: 参数没有输出,说明,这种写法[‘id’ =& 1], 他当成了true,所以等价于4
6://获取某地址的 – 绝对路径 (传输协议-http)
$url = Url::toRoute(‘site/index’, ‘http’);
例如: https://daxia./mobile/hmConnections/site/index
说明: 等价于4
7://获取某地址的 – 绝对路径 (传输协议-https)
$url = Url::toRoute(‘site/index’, ‘https’);
例如: https://daxia./mobile/hmConnections/site/index
[Url::to] – 创建一个基于给定参数的网址 =& 现在测试本地路径(http://daxia./mobile/hmConnections/user/verify-user)
1): //获某网址 – 相对路径
$url = Url::to([‘site/index’]);
举例:/mobile/hmConnections/site/index
说明:等价于2
2): //获取网址(带参数) – 相对路径
$url = Url::to([‘site/index’, ‘id’ =& 1]);
举例:/mobile/hmConnections/site/index?id=1
说明:等价于3
3): 获取当前路径 – 相对路径
$url = Url::to();
举例:/mobile/hmConnections/user/verify-user
4): 获取url – 相对路径
$url = Url::to(‘@web/image/1.jpg’);
举例: /image/a.jpg
说明:它将指定到你的某一个别名目录下@web
5): 获取url – 相对路径
$url = Url::to(‘image/1.jpg’);
举例:image/a.jpg
6): 获取url – 绝对路径(@mobileUrl 自己配置好的)
$url = Url::to(‘@mobileUrl/image/1.jpg’, true);
举例:http://daxia./static/mobile/image/1.jpg
7): //获取url – 绝对路径 (传输协议-https)
$url = Url::to(‘@mobileUrl/image/1.jpg’, ‘https’);
举例:https://daxia./static/mobile/image/1.jpg
8): //获取url – 绝对路径 (传输协议-http)
$url = Url::to(‘@mobileUrl/image/1.jpg’, ‘http’);
举例:http://daxia./static/mobile/image/1.jpg
说明:等价于 6)
** 特别说明下:@mobileUrl
$url = Url::to(‘@mobileUrl/city-partner/city-partner/images/1.png’);
– @mobileUrl,配置如下: Yii::setAlias(‘@mobileUrl’, Yii::getAlias(‘@web/static/mobile/’));
* 给Yii::getAlias(‘@web/static/mobile/’)定义一个别名@mobileUrl(
也就是,下次我们直接用@mobileUrl来表示Yii::getAlias(‘@web/static/mobile/’)的意思)
* @web指的是当前项目目录下的web下, 这是框架默认的
* 而当前目录也需要配置,一般是在common/config/bootstrap.php进行配置
配置如下:Yii::setAlias(‘service’, dirname(dirname(__DIR__)) . ‘/platform_service’);
如需转载请注明: 转载自
本文链接地址:
转载请注明: &如何把视图的参数传递给控制器的操作执行 - 问答 - Yii Framework 中文社区
如何把视图的参数传递给控制器的操作执行
2745次浏览
悬赏 30 金钱
假如有一个用户表,用gii生成CRUD后,把gii生成的index视图文件给改了,不使用GridView小部件,使用自己写的index,用“修改”,“删除”代替GridView生成的小图标,当点击“删除”时,可以删除对应的id的用户。
你的描述是可行的。
共 4 条回复
谢谢你的回答,我已经解决了这个问题。
在gridview中可以自定义函数处理每条记录。本站也有不少例子.
最近忙于找工作,没及时回复,因为之前是参照别人的代码试了下可行,就不管了,想把答案写出来有点乱。今天我重头做了一遍,我使用的是一yii2高级版(advanced)步骤如下:1、我创建了一张member表 2、我用gii生成model路径是@common/models
3、用gii生成CRUD 路径是@backend/views/member
代码如下:控制器:添加 use yii\data\Pagina
重写actionIndex()方法:public function actionIndex()
$query = MemberModel::find();
$pagination = new Pagination([
'defaultPageSize' =& 2,
'totalCount' =& $query-&count(),
$members = $query-&orderBy('name')
-&offset($pagination-&offset)
-&limit($pagination-&limit)
return $this-&render('index', [
'members' =& $members,
'pagination' =& $pagination,
//视图:主要是重写了index视图,代码如下:
use yii\helpers\H
use yii\helpers\U
use yii\widgets\LinkP
?&会员表&thead&
&th&序号 &/th&
&th&名称 &/th&
&th&邮箱 &/th&
&th&操作 &/th&
&?php foreach ($members as $member): ?&
&td &&?= Html::encode($member-&id) ?&&/td&
&td &&?= Html::encode($member-&name) ?&&/td&
&td &&?= Html::encode($member-&email) ?&&/td&
&a href= "&?= Url::to([ 'view', 'id'=&$member-&id]); ?&" &查看 &/a &
&a href= "&?= Url::to([ 'update', 'id'=&$member-&id]); ?&" &编辑 &/a &
&a class ="del" data-method="post" data-confirm="你确定删除该角色或权限吗?" href= "&?= Url::to([ 'delete', 'id'=&$member-&id]); ?&"&删除&/a &
&?= LinkPager::widget(['pagination' =& $pagination]) ?&
至于一些英文改中文就自己改吧
您需要登录后才可以回答。 |Yii 控制层&-&视图 跳转调用详解(一)
我的图书馆
Yii 控制层&-&视图 跳转调用详解(一)
Yii控制层处理结果返回前端的三种方式&作者:zccst&批注:render会渲染layout,而renderPartial不会渲染。&一、渲染方式&1,局部渲染renderPartial&public function renderPartial($view,$data=null,$return=false,$processOutput=false)&{&&&&&& if(($viewFile=$this-&getViewFile($view))!==false)&&& {&&&& $output=$this-&renderFile($viewFile,$data,true);//getViewFile($view)获得$view的完整路径&&&& if($processOutput)&&&&& $output=$this-&processOutput($output);& // processOutput()作用,比如在head加上css或js脚本等&&&& if($return)&&&&& return $&&&& else&&&&& echo $&&& }&&& else&&&& throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',&&&&& array('{controller}'=&get_class($this), '{view}'=&$view)));&}&注解:&(1)getViewFile($view)获得$view的完整路径&(2)如果没有在$config里配置第三方的renderer,renderFile() 里实际是调用了yii自身提供的renderInternal()来render view文件:&public function renderFile($viewFile,$data=null,$return=false)&{&&& $widgetCount=count($this-&_widgetStack);&&& // 如果配置了其他的ViewRenderer&&& if(($renderer=Yii::app()-&getViewRenderer())!==null)&&&& $content=$renderer-&renderFile($this,$viewFile,$data,$return);&&& else&&&& // yii 自身的render&&&& $content=$this-&renderInternal($viewFile,$data,$return);&&& if(count($this-&_widgetStack)===$widgetCount)&&&& return $&&& else&&& {&&&& $widget=end($this-&_widgetStack);&&&& throw new CException(Yii::t('yii','{controller} contains improperly nested widget tags in its view "{view}". A {widget} widget&does not have an endWidget() call.',&&&&& array('{controller}'=&get_class($this), '{view}'=&$viewFile, '{widget}'=&get_class($widget))));&&& }&}&Yii的renderer用的是php本身作为模板系统:&public function renderInternal($_viewFile_,$_data_=null,$_return_=false)&{&&& // extract函数将$_data_从数组中将变量导入到当前的符号表&&& if(is_array($_data_))&&&& extract($_data_,EXTR_PREFIX_SAME,'data');&&& else&&&& $data=$_data_;&&& if($_return_)&&& {&&&& ob_start();&&&& ob_implicit_flush(false);&&&& require($_viewFile_);&&&& return ob_get_clean();&&& }&&& else&&&& require($_viewFile_);&}&2,全局渲染render&render()的实际上是先renderPartial view文件,然后renderFile layoutfile,并将view文件的结果做为$content变量传入。&public function render($view,$data=null,$return=false)&{&&& $output=$this-&renderPartial($view,$data,true);&&& if(($layoutFile=$this-&getLayoutFile($this-&layout))!==false)&&&& $output=$this-&renderFile($layoutFile,array('content'=&$output),true);&&& $output=$this-&processOutput($output);&&& if($return)&&&& return $&&& else&&&& echo $&}&processOutput将render的结果再做处理,比如在head加上css或js脚本等。&public function processOutput ($output)&{&&& Yii::app()-&getClientScript()-&render($output);&&& // if using page caching, we should delay dynamic output replacement&&& if($this-&_dynamicOutput!==null && $this-&isCachingStackEmpty())&&&& $output=$this-&processDynamicOutput($output);&&& if($this-&_pageStates===null)&&&& $this-&_pageStates=$this-&loadPageStates();&&& if(!empty($this-&_pageStates))&&&& $this-&savePageStates($this-&_pageStates,$output);&&& return $&}&区别:&render会把需要的js,css等嵌入&renderPartial可以通过把最后一个参数设置成true完成一样的功能 $this-&renderPartial('partial_view', $params, false, true);&实例:&在../controllers/XXController.php中&$this-&render('update',array('model'=&$model));&在../views/ControllerID/update.php中&&?php echo $this-&renderPartial('_form', array('model'=&$model)); ?&&二、暴力跳转模式——适合form表单提交&$this-&redirect(array('view','id'=&$model-&id));&三、返回值方式——适合异步调用&//写法1:&public function response($data, $type="application/json"){&&&& print json_encode($data);&&&& Yii::app()-&end();&}&//写法2:&$this-&layout =&header('Content-type: application/json');&echo json_encode($arr);&Yii::app()-&end();& //执行该行,已经将layout设为了false&Yii中render和renderPartial的区别本文主要为大家讲解了Yii框架中render和renderPartial的区别,感兴趣的同学参考下.在进行页面输出渲染的时候区别:1.render 输出父模板的内容,将渲染的内容,嵌入父模板。|2.renderPartial 则不输出父模板的内容。只对本次渲染的局部内容,进行输出。同时还有个重要的区别:render 函数内部默认执行processOutput($output)函数, 会将把组件,比如 CTreeView 里面注册到 CClientScript 里面的需要的脚本进行渲染输出。而renderPartial() 默认不自动渲染输出客户端脚本,需要进行参数的指定,才会输出:renderPartial($view,$data=null,$return=false,$processOutput=false)指定processOutput 为 true 即可。比如要局部输出 CTreeView ,用renderPartial 进行渲染,如果按照默认processOutput=false 则输出内容,不含有客户端脚本输出内容则为 正常的 ul 列表。没有树形的折叠效果。 主动设定 processOutput=true 后,CTreeView 所需的,所有客户端脚本就会被正常输出在列表的前面。下面介绍下要用到的几个相关的函数:render,renderPartial 不再介绍processOutput()&?php
publicfunction render($view,$data=null,$return=false)
if($this-&beforeRender($view))
$output=$this-&renderPartial($view,$data,true);
if(($layoutFile=$this-&getLayoutFile($this-&layout))!==false)
$output=$this-&renderFile($layoutFile,array('content'=&$output),true);
$this-&afterRender($view,$output);
$output=$this-&processOutput($output);
if($return)
publicfunction renderPartial($view,$data=null,$return=false,$processOutput=false)
if(($viewFile=$this-&getViewFile($view))!==false)
$output=$this-&renderFile($viewFile,$data,true);
if($processOutput)
$output=$this-&processOutput($output);
if($return)
thrownewCException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
array('{controller}'=&get_class($this),'{view}'=&$view)));
publicfunction processOutput($output)
Yii::app()-&getClientScript()-&render($output);
// if using page caching, we should delay dynamic output replacement
if($this-&_dynamicOutput!==null&& $this-&isCachingStackEmpty())
$output=$this-&processDynamicOutput($output);
$this-&_dynamicOutput=
if($this-&_pageStates===null)
$this-&_pageStates=$this-&loadPageStates();
if(!empty($this-&_pageStates))
$this-&savePageStates($this-&_pageStates,$output);
}以上在实际操作中还是比较有用的,比如你不想用大组建,可以直接将变量输到模板,也可以将多个变量组成数组输到模版里面去.
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢Yii2.0教程应用结构篇 - 控制器 - 教程 - Yii Framework 中文社区
Yii2.0教程应用结构篇 - 控制器
6579次浏览
控制器是 MVC 模式中的一部分, 是继承yii\base\Controller类的对象,负责处理请求和生成响应。 具体来说,控制器从应用主体接管控制后会分析请求数据并传送到模型, 传送模型结果到视图,最后生成输出响应信息。
控制器由动作组成,它是执行终端用户请求的最基础的单元,一个控制器可有一个或多个动作。
如下示例显示包含两个动作view and create 的控制器post:
namespace app\
use app\models\P
use yii\web\C
use yii\web\NotFoundHttpE
class PostController extends Controller
public function actionView($id)
$model = Post::findOne($id);
if ($model === null) {
throw new NotFoundHttpE
return $this-&render('view', [
'model' =& $model,
public function actionCreate()
$model = new P
if ($model-&load(Yii::$app-&request-&post()) && $model-&save()) {
return $this-&redirect(['view', 'id' =& $model-&id]);
return $this-&render('create', [
'model' =& $model,
在操作 view (定义为 actionView() 方法)中, 代码首先根据请求模型ID加载 模型, 如果加载成功,会渲染名称为view的视图并显示,否则会抛出一个异常。
在操作 create (定义为 actionCreate() 方法)中, 代码相似. 先将请求数据填入模型, 然后保存模型,如果两者都成功,会跳转到ID为新创建的模型的view动作,否则显示提供用户输入的create视图。
终端用户通过所谓的路由寻找到操作,路由是包含以下部分的字符串:
模型ID: 仅存在于控制器属于非应用的模块;
控制器ID: 同应用(或同模块如果为模块下的控制器)下唯一标识控制器的字符串;
动作ID: 同控制器下唯一标识动作的字符串。
路由使用如下格式:ControllerID/ActionID
如果属于模块下的控制器,使用如下格式:ModuleID/ControllerID/ActionID
如果用户的请求地址为 http://hostname/index.php?r=site/index , 会执行site 控制器的index 操作。 更多关于处理路由的详情请参阅 路由 一节。
创建控制器
在yii\web\Application网页应用中,控制器应继承yii\web\Controller 或它的子类。 同理在yii\console\Application控制台应用中,控制器继承yii\console\Controller 或它的子类。 如下代码定义一个 site 控制器:
namespace app\
use yii\web\C
class SiteController extends Controller
通常情况下,控制器用来处理请求有关的资源类型,因此控制器ID通常为和资源有关的名词。 例如使用article作为处理文章的控制器ID。
控制器ID应仅包含英文小写字母、数字、下划线、中横杠和正斜杠, 例如 article 和 post-comment 是真是的控制器ID,article?,PostComment, admin\post不是控制器ID。
控制器Id可包含子目录前缀,例如 admin/article 代表 yii\base\Application::controllerNamespace 控制器命名空间下 admin子目录中 article 控制器。 子目录前缀可为英文大小写字母、数字、下划线、正斜杠,其中正斜杠用来区分多级子目录(如 panels/admin)。
控制器类命名
控制器ID遵循以下规则衍生控制器类名:
将用正斜杠区分的每个单词第一个字母转为大写。注意如果控制器ID包含正斜杠,只将最后的正斜杠后的部分第一个字母转为大写;
去掉中横杠,将正斜杠替换为反斜杠;
增加Controller后缀;
在前面增加yii\base\Application::controllerNamespace控制器命名空间.
下面为一些示例,假设yii\base\Application::controllerNamespace控制器命名空间为 app\controllers:article 对应 app\controllers\ArticleC
post-comment 对应 app\controllers\PostCommentCadmin/post-comment 对应 app\controllers\admin\PostCommentCadminPanels/post-comment 对应 app\controllers\adminPanels\PostCommentController.
控制器类必须能被 自动加载,所以在上面的例子中, 控制器article 类应在 别名 为@app/controllers/ArticleController.php的文件中定义, 控制器admin/post2-comment应在@app/controllers/admin/Post2CommentController.php文件中。
补充: 最后一个示例 admin/post2-comment 表示你可以将控制器放在 yii\base\Application::controllerNamespace控制器命名空间下的子目录中, 在你不想用 模块 的情况下给控制器分类,这种方式很有用。
控制器部署
可通过配置 yii\base\Application::controllerMap 来强制上述的控制器ID和类名对应, 通常用在使用第三方不能掌控类名的控制器上。
配置 应用配置 中的application configuration,如下所示:
'controllerMap' =& [
// 用类名申明 "account" 控制器
'account' =& 'app\controllers\UserController',
// 用配置数组申明 "article" 控制器
'article' =& [
'class' =& 'app\controllers\PostController',
'enableCsrfValidation' =& false,
默认控制器
每个应用有一个由yii\base\Application::defaultRoute属性指定的默认控制器; 当请求没有指定 路由,该属性值作为路由使用。 对于yii\web\Application网页应用,它的值为 'site', 对于 yii\console\Application控制台应用,它的值为 help, 所以URL为 http://hostname/index.php 表示由 site 控制器来处理。
可以在 应用配置 中修改默认控制器,如下所示:
'defaultRoute' =& 'main',
创建动作可简单地在控制器类中定义所谓的 动作方法 来完成,动作方法必须是以action开头的公有方法。 动作方法的返回值会作为响应数据发送给终端用户,如下代码定义了两个动作 index 和 hello-world:
namespace app\
use yii\web\C
class SiteController extends Controller
public function actionIndex()
return $this-&render('index');
public function actionHelloWorld()
return 'Hello World';
动作通常是用来执行资源的特定动作,因此,动作ID通常为动词,如view, update等。
动作ID应仅包含英文小写字母、数字、下划线和中横杠,动作ID中的中横杠用来分隔单词。 例如view, update2, comment-post是真实的操作ID,view?, Update不是操作ID.
可通过两种方式创建动作ID,内联动作和独立动作。内联动作在控制器类中定义为方法;独立动作是继承yii\base\Action或它的子类的类。 内联动作容易创建,在无需重用的情况下优先使用; 独立动作相反,主要用于多个控制器重用,或重构为扩展。
内联动作指的是根据我们刚描述的动作方法。
动作方法的名字是根据操作ID遵循如下规则衍生:
将每个单词的第一个字母转为大写;
去掉中横杠;
增加action前缀.
例如index 转成 actionIndex, hello-world 转成 actionHelloWorld。
注意: 操作方法的名字大小写敏感,如果方法名称为ActionIndex不会认为是操作方法, 所以请求index操作会返回一个异常,也要注意操作方法必须是公有的,私有或者受保护的方法不能定义成内联操作。
因为容易创建,内联动作是最常用的动作,但是如果你计划在不同地方重用相同的动作, 或者你想重新分配一个动作,需要考虑定义它为独立动作。
独立动作通过继承yii\base\Action或它的子类来定义。 例如Yii发布的yii\web\ViewAction和yii\web\ErrorAction都是独立操作。
要使用独立动作,需要通过控制器中覆盖yii\base\Controller::actions()方法在action map中申明,如下例所示:
public function actions()
// 用类来申明"error" 操作
'error' =& 'yii\web\ErrorAction',
// 用配置数组申明 "view" 操作
'view' =& [
'class' =& 'yii\web\ViewAction',
'viewPrefix' =& '',
如上所示, actions() 方法返回键为动作ID、值为对应动作类名或数组configurations 的数组。 和内联动作不同,独立动作ID可包含任意字符,只要在actions() 方法中申明.
为创建一个独立动作类,需要继承yii\base\Action 或它的子类,并实现公有的名称为run()的方法, run() 方法的角色和动作方法类似,例如:
namespace app\
use yii\base\A
class HelloWorldAction extends Action
public function run()
return "Hello World";
动作方法或独立动作的run()方法的返回值非常重要,它表示对应动作结果。
返回值可为 响应 对象,作为响应发送给终端用户。
对于yii\web\Application网页应用,返回值可为任意数据, 它赋值给yii\web\Response::data, 最终转换为字符串来展示响应内容。
对于yii\console\Application控制台应用,返回值可为整数, 表示命令行下执行的 yii\console\Response::exitStatus 退出状态。
在上面的例子中,动作结果都为字符串,作为响应数据发送给终端用户,下例显示一个动作通过 返回响应对象(因为yii\web\Controller::redirect()方法返回一个响应对象)可将用户浏览器跳转到新的URL。
public function actionForward()
// 用户浏览器跳转到
return $this-&redirect('');
内联动作的动作方法和独立动作的 run() 方法可以带参数,称为动作参数。 参数值从请求中获取,对于yii\web\Application网页应用, 每个动作参数的值从$_GET中获得,参数名作为键; 对于yii\console\Application控制台应用, 动作参数对应命令行参数。
如下例,动作view (内联动作) 申明了两个参数 $id 和 $version。
namespace app\
use yii\web\C
class PostController extends Controller
public function actionView($id, $version = null)
动作参数会被不同的参数填入,如下所示:http://hostname/index.php?r=post/view&id=123: $id 会填入'123',$version 仍为 null 空因为没有version请求参数;http://hostname/index.php?r=post/view&id=123&version=2: $id 和 $version 分别填入 '123' 和 '2';http://hostname/index.php?r=post/view: 会抛出yii\web\BadRequestHttpException 异常 因为请求没有提供参数给必须赋值参数$id;http://hostname/index.php?r=post/view&id[]=123: 会抛出yii\web\BadRequestHttpException 异常 因为$id 参数收到数字值['123']而不是字符串.
如果想让动作参数接收数组值,需要指定$id为array,如下所示:
public function actionView(array $id, $version = null)
现在如果请求为 http://hostname/index.php?r=post/view&id[]=123, 参数 $id 会使用数组值['123'], 如果请求为http://hostname/index.php?r=post/view&id=123, 参数 $id 会获取相同数组值,因为无类型的'123'会自动转成数组。
上述例子主要描述网页应用的操作参数,对于控制台应用,更多详情请参阅控制台命令。
每个控制器都有一个由 yii\base\Controller::defaultAction 属性指定的默认动作, 当路由 只包含控制器ID,会使用所请求的控制器的默认操作。
默认动作默认为 index,如果想修改默认动作,只需简单地在控制器类中覆盖这个属性,如下所示:
namespace app\
use yii\web\C
class SiteController extends Controller
public $defaultAction = 'home';
public function actionHome()
return $this-&render('home');
控制器生命周期
处理一个请求时,应用主体 会根据请求路由创建一个控制器,控制器经过以下生命周期来完成请求:
在控制器创建和配置后,yii\base\Controller::init() 方法会被调用。
控制器根据请求操作ID创建一个操作对象:
如果操作ID没有指定,会使用yii\base\Controller::defaultAction默认操作ID;
如果在yii\base\Controller::actions()找到操作ID,会创建一个独立操作;
如果操作ID对应操作方法,会创建一个内联操作;
否则会抛出yii\base\InvalidRouteException异常。
控制器按顺序调用应用主体、模块(如果控制器属于模块)、控制器的 beforeAction() 方法;
如果任意一个调用返回false,后面未调用的beforeAction()会跳过并且操作执行会被取消; action execution will be cancelled.
默认情况下每个 beforeAction() 方法会触发一个 beforeAction 事件,在事件中你可以追加事件处理操作;
控制器执行操作:
请求数据解析和填入到操作参数;
控制器按顺序调用控制器、模块(如果控制器属于模块)、应用主体的 afterAction() 方法;
默认情况下每个 afterAction() 方法会触发一个 afterAction 事件,在事件中你可以追加事件处理操作;
应用主体获取操作结果并赋值给响应.
在设计良好的应用中,控制器很精练,包含的操作代码简短; 如果你的控制器很复杂,通常意味着需要重构,转移一些代码到其他类中。
归纳起来,控制器
可访问 请求 数据;
可根据请求数据调用 模型 的方法和其他服务组件;
可使用 视图 构造响应;
不应处理应被模型处理的请求数据;
应避免嵌入HTML或其他展示代码,这些代码最好在 视图中处理.
这是一篇不错的文章,但是作者markdown用的不过关,希望再好好编辑一下。
这个是文档里面的吧
您需要登录后才可以评论。 |

我要回帖

更多关于 yii2 视图 foreach 的文章

 

随机推荐