yii2框架初识

架构

这里写图片描述

工作流

这里写图片描述

控制器

规则:

  • 控制器的文件名首字母大写,以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)

这里写图片描述

这里写图片描述

视图

控制器与视图对应, 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值访问。

关于数据库的操作,后续添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值