1、将根目录指向 域名/web/ 出现open_basedir错误
6、API跨域的问题
14、easywechat报错Symfony\Contracts\EventDispatcher\EventDispatcherInterface
15、yii2 Expected response code 220 but got an empty response
1、将根目录指向 域名/web/ 出现open_basedir错误
打开apache/httpd-vhosts.conf
搜索:
ServerName 本地解析的域名
将open_basedir注释掉
比如 / 被转义成了 %2F
修改文件:
/vendor/yiisoft/yii2/web/UrlManager.php
丢掉urlencode即可
默认的controller为SiteController.php
在控制器中加一行:
- public $defaultAction='login';
composer.json去掉要删除的扩展或者库
运行
- composer update -vvv
- composer dump-autoload
- composer update
- $userHost = Yii::$app->request->userHost;
- $userIP = Yii::$app->request->userIP;
- 链接:
- http://gzh.com/course/app/list?mode=hot
- echo \Yii::$app->request->hostInfo;
- //## http://gzh.com
- echo \Yii::$app->request->getUrl();
- //## /course/app/list?mode=hot
- echo \Yii::$app->request->getAbsoluteUrl();
- //## http://gzh.com/course/app/list?mode=hot
- echo \Yii::$app->request->pathInfo;
- //## course/app/list
- $moduleID = \Yii::$app->controller->module->id;
- $controllerID = \Yii::$app->controller->id;
- $actionID = \Yii::$app->controller->action->id;
6、API跨域的问题
如果是Google浏览器,打开chrome://flags/#out-of-blink-cors
两种情况,
第一种,在基类controller中,如api/common/Controller.php中的
第二种情况
- $response = $event->sender;
- $response->data = [
- 'code' => $response->statusCode,
- 'message' => $response->statusText,
- 'data' => $response->data,
- ];
这里不能用array_merge了
7、自动加载类文件,在YII里面,由于底层目录被改的乱七八糟,已经无法使用use来引入类文件了,这里可以放在use之前 ,或之后 都可以,根据加载的类来引入文件
- define("DIR_SERVICE",__DIR__);
- /* 自动加载类文件 */
- spl_autoload_register(function ($class) {
- if ($class) {
- echo $class.'<br>'; // 命名空间:service\widgets\models\Widget
- $file = str_replace('service', DIR_SERVICE, $class);
- $file .= '.class.php';
- echo $file; // 替换service以后 E:\\www\web\widgets\models\Widget
- die;
- require($file);
- }
- });
Apache目录 /conf/httpd-vhosts.conf
重启 apache
- <?php
- //常规的渲染页面:
- $searchModel = new LifeSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('/life/index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- //自定义条件,或搜索字段名与表中不一致的:
- $searchModel = new LifeSearch();
- $search = [
- $searchModel->formName() =>
- [
- 'plan_id' => $id
- ]
- ];
- $dataProvider = $searchModel->search($search);
- return $this->render('/life/index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- // 当然,在searchModel中,对于要搜索的字段,rules中请加safe规则
- public function rules()
- {
- return [
- [['id', 'plan_id'], 'safe']
- ];
- }
新建一个Behavior
- <?php
- namespace app\common\behaviors;
- use Yii;
- use yii\base\ActionEvent;
- use yii\base\Behavior;
- use yii\web\Controller;
- class NoCsrf extends Behavior
- {
- public $actions = [];
- public $controller;
- public function events()
- {
- return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
- }
- public function beforeAction($event)
- {
- $action = $event->action->id;
- if(in_array($action, $this->actions)){
- $this->controller->enableCsrfValidation = false;
- }
- }
- }
在控制器中use:
- use app\common\behaviors\NoCsrf;
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post','get'],
- ],
- ],
- 'csrf' => [
- 'class' => NoCsrf::className(),
- 'controller' => $this,
- 'actions' => [
- 'switcher'
- ]
- ]
- ];
- }
或者在 model中添加:
- public function beforeAction($action)
- {
- if ($action->id == 'my-method') {
- $this->enableCsrfValidation = false;
- }
- return parent::beforeAction($action);
- }
一、
- public function actionSwitcher(){
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- return [
- 'search' => "11",
- 'code' => 100,
- ];
- }
二、
- public function actionSwitcher(){
- $test = [
- 'code' => '200',
- 'abc' => '测试',
- ];
- return \yii\helpers\Json::encode($test);
- }
三、在controller中指定方法返回json:
- use yii\filters\ContentNegotiator;
- use yii\web\Response;
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'contentNegotiator' => [
- 'class' => ContentNegotiator::className(),
- 'formats' => [
- 'application/json' => Response::FORMAT_JSON,
- ],
- 'only' => ['switcher'],
- ],
- ];
- }
- public function actionSwitcher(){
- $test = [
- 'code' => '200',
- 'abc' => '测试',
- ];
- return $test;
- }
如果在module中的话 就要['控制器/动作']
yii2默认在执行完__construct()后,执行init() 函数 ,所以,自己添加的类,可以加方法:
- function init(){
- //...
- }
重写init,beforeAction,根据你的需要。初始化变量建议重写init
方案1:控制器内成员变量
- public $layout = false; //不使用布局public $layout = "main"; //设置使用的布局文件
方案2:控制器成员方法内
- $this->layout = false; //不使用布局
- $this->layout = "main"; //设置使用的布局文件
方案3:视图中选择布局
- $this->context->layout = false; //不使用布局
- $this->context->layout = 'main'; //设置使用的布局文件
方案4:在controller中指定的方法中不使用布局文件的写法:
- return $this->renderPartial('index',$data);//第二个参数用来传递数组给模板文件
14、easywechat报错Symfony\Contracts\EventDispatcher\EventDispatcherInterface
升级到php7.2.9 +
或者
- composer require symfony/event-dispatcher:~4.3
15、yii2发送邮件:yii2 Expected response code 220 but got an empty response
如果使用ssl端口,则encyption配置项必须为465/994,如果使用非ssl则应该使用25