0
点赞
收藏
分享

微信扫一扫

YII框架


1.安装,学习

​​https://www.yiichina.com/doc/guide/2.0/start-workflow​​

安装高级版本 composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-advanced yii-application

 

 

添加组件

1.使用composer,如

composer require yiisoft/yii2-redis

对应的在basic\vendor\yiisoft\extensions.php中会自动添加如下配置,注意路径: '/yiisoft/yii2-redis/src', 应该是指到源码路径这一层

'yiisoft/yii2-redis' => 
array (
'name' => 'yiisoft/yii2-redis',
'version' => '2.0.13.0',
'alias' =>
array (
'@yii/redis' => $vendorDir . '/yiisoft/yii2-redis/src',
),
),

2 .controller

可以添加独立的controller而没有 model和view

如要添加: admin\api\下的  PostCommentController,该类的命名空间是

namespace app\controllers\admin\api;

访问的url是 ​​http://localhost:8080/index.php?r=admin/api/post-comment/index​​

Module Class可以自己任意命名,   如:   app\modules\net\myrpc,,不一定要是app\modules\net\Module

 

3. Gii运行不起来,报错:

 The configuration for the "modules" component must contain a "class" element.

很可能是$config   modules 中加载了有错误的 modules,

'modules' => [  要和  components同一个级别,不能配置在components里

 

4. 修改session不为默认名PHPSESSID

在打开session之前设置名字, 

$session->setName("session");

$session->open();

 

 

2. restful

如果要在controller中实现自定义的方法,要现在action中unset这个操作,然后再实现自定义方法

public function actions()
{
$action= parent::actions(); // TODO: Change the autogenerated stub
unset($action['index']);
unset($action['create']);
unset($action['update']);
unset($action['delete']);
}

比如delete操作对应 public function actionDelete()

[
'PUT,PATCH users/<id>' => 'user/update',
'DELETE users/<id>' => 'user/delete',
'GET,HEAD users/<id>' => 'user/view',
'POST users' => 'user/create',
'GET,HEAD users' => 'user/index',
'users/<id>' => 'user/options',
'users' => 'user/options',
]

 

举报

相关推荐

0 条评论