>我有以下目录结构
- 根/
- 根/受保护
- 根/框架
- 根/后端
- 根/后端/受保护
好吧,第一个 Yii 应用程序直接位于与框架文件夹同一级别的 root 内部。第二个 Yii 应用程序位于backend
文件夹中。它们都使用位于 root
中的相同框架文件夹。
我在两个应用程序中都使用路径样式 url。 像mydomain.com/controller/action
.
问题是..
但是当我尝试打开时 mydomain.com/backend/
Yii 试图将backend
作为控制器名称传递给 mydomain.com/index.php
,并给了我 404。但实际上它是单独应用程序的子目录
我试图解决这个问题:将root/index.php
的内容更改为以下代码:
<?php
if (strpos($_SERVER["REQUEST_URI"], 'backend') !== false) {
require_once dirname(__FILE__) . '/backend/index.php';
} else {
// change the following paths if necessary
$yii = dirname(__FILE__) . '/framework/yii.php';
$config = dirname(__FILE__) . '/protected/config/main.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
require_once($yii);
Yii::createWebApplication($config)->run();
}
它有效,但所有媒体、css 文件路径都搞砸了:例如.css位于backend/css
内部尝试打开 mydomain.com/img.jpg 而不是mydomain.com/backend/img.jpg
。
换句话说,我尝试的不是正确的解决方案。我的网络服务器是NGINX,配置如下所示:
server {
set $host_path "/var/www/mydomain";
server_name mydomain.com;
root $host_path;
set $yii_bootstrap "index.php";
charset utf-8;
location / {
index index.html $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args;
}
location /backend/ {
index index.html $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args;
}
location ~ ^/(protected|framework|themes/'w+/views) {
deny all;
}
location ~ '.php$ {
fastcgi_split_path_info ^(.+'.php)(.*)$;
#let yii catch the calls to unexising PHP files
set $fsn /$yii_bootstrap;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
location ~ /'. {
deny all;
access_log off;
log_not_found off;
}
}
您不必对核心进行任何类型的更改。最好尝试路由。
像这样的东西——
"/后端/控制器/操作" => 主配置中的"/控制器/操作/.php"。
希望这有帮助...