安装:

配置cookievalidationkey,为了防止cookie攻击,随便配置一个值

'cookieValidationKey' => 'aaa',

 

命名空间:

没有命名空间namespace的类是全局类,重命名相同类用as

访问全局类

访问

控制层以controller结尾,方法以action开头

 

访问地址   http://php/yii/web/index.php?r=hello/index

hello是控制器名称,index是该类中的方法

//////////////////////请求组件

获得地址参数

Get方式:

http://php/yii/web/index.php?r=hello/index&id=1

$request=\Yii::$app->request;
//若id参数不存在,返回get的第二个参数
$id=$request->get('id',100);

Post方式:

$id=$request->post('id',100);

//////////////////////响应组件

设置头部信息

$request=\Yii::$app->response;

$request->headers->add('pragma','no-cache');
//        缓存5秒钟
$request->headers->add('pragma','max-age=5');
//        删除设置的头部信息
$request->headers->remove('pragma');

跳转
$this->redirect('http://www.baidu.com',302);

下载文件,需要下载的文件路径和入口文件路径一致

Session
        $request=\Yii::$app->session;
// 判断session是否开启  $request->isActive
// 开启   $request->open();

查看session存放路径

phpstudy中php.ini查找  session.save_path

 

对象方法

//        增加
        $request->set('name','crx');
//        获取
        $request->get('name');
//        删除
        $request->remove('name');

或者 数组方法

 

Cookie
        $request=\Yii::$app->response->cookies;
//        增加
        $data=array('name'=>'user','value'=>'crx');
        $request->add(new Cookie($data));
//        获取
        echo $request->get('user')."===";
//        删除


//        $request->remove('user');

  

视图的创建

return $this->renderPartial('index',['str'=>'hello','num'=>array(1,2)  ]);

 

在视图中两种显示数据的方法

<?php echo $a; ?>


<?=$a; ?>

 

导入js 或者css的方法

<?php
use yii\helpers\Html;
?>
<?=Html::jsFile('@web/assets/index/js/index.js')?>
<?=Html::cssFile('@web/assets/index/css/bottom.css')?>

 

显示图片的方法

<img src="<?=Url::to('@web/assets/index/img/2.png')?>"/>

 

视图层连接到指定的控制层的方法中

<a href="<?=Url::toRoute(['/index/default/test', 'id' => 42])?>">sssss</a>

 

公共视图模板

public $layout='common';

return $this->render('hello');

显示其他模板数据 

路由配置

隐藏入口文件

.htcaccess文件拷贝到项目的web文件夹中

web的文件夹下增加.htcaccess文件 里面增加重写代码

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]
</IfModule>

config文件夹中web.php中开启这段语句,默认是注释的

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],

 

设置伪静态后缀名

http://localhost/PHP/phpBase/yii/web/hello/hello.html

hello控制器名称,hello控制器下的方法名称,.html是伪后缀

 

数据库操作

gii创建

Gii网址

http://localhost/PHP/phpBase/yii/web/gii

app\modules\admin\admin

即创建成功

然后在config/web.php中做配置

$config['modules']['admin'] = [
    'class' => 'app\modules\admin\admin',
];

 

访问路径:

http://localhost/PHP/phpBase/yii/web/admin

 

//////////////////////Vue+yii

需要解决跨域的问题,只要在对应的controller中加入头设置即可

 header("Access-Control-Allow-Origin:*");

XSS漏洞

$a=\Yii::$app->request->get('a');
//        转码,转为常规的字符串
$str=Html::encode($a);
//        过滤script标签
$str1=HtmlPurifier::process($a);