5分钟教你配置Yii2程序支持虚拟主机 - Yii2高级版

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyb8010/article/details/87347843

现在服务端程序要求提供api模式,所以前段时间学习了yii2高级版,但是现在很多程序还是运行在虚拟主机上,所以怎么将yii2高级版的http://www.***.com:/frontend/web/index.php?r=site%2Flogin这种模式改为:http://www.***.com:/index.php?r=site%2Flogin呢。以下是我的测试:

一、将frontend/web/的程序复制到根目录下

二、将index.php的程序做如下修改:

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/common/config/bootstrap.php';
require __DIR__ . '/frontend/config/bootstrap.php';

$config = yii\helpers\ArrayHelper::merge(
    require __DIR__ . '/common/config/main.php',
    require __DIR__ . '/common/config/main-local.php',
    require __DIR__ . '/frontend/config/main.php',
    require __DIR__ . '/frontend/config/main-local.php'
);

(new yii\web\Application($config))->run();

三、将frontend下的目录web目录删除。

四、在将api端实现为:www.***.com/api/web/site/login的模式时,发现实现不了,好像是调用不到index.php及控制器也调用不了。于是做了各种猜想,一步一步排除法排查问题,排除法是在不知底层的情况下发现问题和解决问题的好办法。与之对应的就是读底层代码,但这样虽然能解决问题,速度会更慢。最后发现可以将.haccess的代码改为以下形式实现了隐藏index.php及实现伪静态:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /api/web/index.php [L]
</IfModule>

五、题外话

在调试api的过程中发现www.***.com/api/web/articles在浏览器里面查看代码时,无法显示xml数据,或者是下载.json的提示,后来发现是选择了360浏览器的兼容模式,改为极速模式就好了。在postman里面测试数据时是成功的。

猜你喜欢

转载自blog.csdn.net/lyb8010/article/details/87347843
今日推荐