yii2框架初识
来源:互联网 发布:mac点不开 app store 编辑:程序博客网 时间:2024/06/10 13:02
架构
工作流
控制器
规则:
- 控制器的文件名首字母大写,以Controller结束
- 控制器的类必须要继承Controller类,use yii\web\Controller
- 处理请求的方法也叫做“操作”,假设某操作名为index。index动作被定义为actionIndex方法。 Yii 使用action前缀区分普通方法和动作。action 前缀后面的名称被映射为动作的 ID。
?如何把请求交给控制器,并由控制器中的某个操作来处理(假设是HelloController类中的actionIndex)
解答:url后附上r=hello/index,表明由hello控制器中的index操作来处理。
?如何在控制器中接受url中附加的参数(假设url为localhost/index.php?r=hello/index&id=3)
首先要了解几个常见的变量:
YII–全局类
$app–静态变量,是应用主体,用于加载应用组件。
访问全局变量前加反斜杠‘\’来访问,例如‘\YII::$app’。
$request = \YII::$app->request;$request->get('id', 20);
如代码所示,get方法的第一个参数为参数名,第二个参数是默认值,在参数没有值时,默认返回第二个参数。post同get。
tips1: 跳转和文件下载
tips2: session(继承ArrayAccess)
tips2: cookie
视图
控制器与视图对应, HelloController对应的视图文件夹为views\hello,要访问该动作下的操作,就在url后加上动作名,不加则默认访问index。(假设有一个 actionDo 方法,则url为http://localhost/testYii/basic/web/index.php?r=hello/do)
? render和renderPartial的区别是什么?
render能渲染布局文件,而renderPartial则只是局部渲染。
让我们来看一下源代码:
render
public function render($view,$data=null,$return=false){ if($this->beforeRender($view)) { $output=$this->renderPartial($view,$data,true);//渲染子模板 if(($layoutFile=$this->getLayoutFile($this->layout))!==false)//将子模版渲染的内容放到content变量中去渲染父模板,在父模板中输出$content $output=$this->renderFile($layoutFile,array('content'=>$output),true); $this->afterRender($view,$output); $output=$this->processOutput($output); if($return) return $output; else echo $output; }}
renderPartial
public function 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) return $output; else echo $output; } else throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".', array('{controller}'=>get_class($this), '{view}'=>$view)));}
重点就在render函数内部默认执行processOutput()函数,而renderPartial函数默认不执行该函数。
layout:
yii为我们提供了抽取公用view的思路,把公有的布局文件抽取出来,放在views\layout文件夹下,需要使用时,如下代码表示使用common.php布局文件。
public $layout = 'common';
再调用render函数进行渲染时,render内部把子模块放到$content中,common.php的代码如下:
<!DOCUTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>testYii</title></head><body><?=$content;?></body></html>
视图数据传递:
render/renderPartial的第二个参数用于数据传递,从动作函数中传递数据到视图页面
public function actionDo(){ $msg = "How do you do?"; $test_arr = array(1, 2); //创建一个数组 $data = array(); $data['view_msg'] = $msg; $data['view_test_arr'] = $test_arr; return $this->renderPartial('index', $data);}
在view中则通过key值来访问:
<?=$view_msg;?>
?如何在一个view中如何显示另一个view呢
- 假设在index.php中想显示about.php的渲染界面,则:
<?php echo $this->render('about',array('a'=>'111'); ?>
同理,第二个参数传参,在about页用key值访问。
关于数据库的操作,后续添加
- yii2框架初识
- Yii2.0 初识 RESTful Serializer
- 安装Yii2框架
- yii2框架的安装
- YII2.0框架分页
- yii2框架安装
- composer 安装yii2框架
- yii2框架-httpclient客户端
- yii2.0框架分页
- Yii2框架表单
- YII2框架详解
- windows安装yii2框架
- yii2框架详解
- YII2框架redis引用
- YII2框架语言包配置
- YII2框架安全篇
- YII2框架总结详解
- Yii2框架随笔
- 用例设计方法
- RAC grid卸载
- Android Rtmp客户端搭建
- java中HashMap实现细节1
- angular2 中使用第三方组件样式调整
- yii2框架初识
- 学习Axure RP原型设计
- HTML中为何p标签内不可包含div标签?那哪些块元素里面不能放哪些块元素呢?
- Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
- Android Studio的Instant Run工作原理及用法
- JS事件监听的添加方法
- Ubuntu下查看linux版本,内核版本,系统位数,gcc版本
- 标准IO下对文件权限的修改
- spark-streaming-[1]-streaming基础NetworkWordCount