【发布时间】:2013-09-08 17:47:59
【问题描述】:
我有以下目录结构
- 根/
- root/受保护
- 根/框架
- 根/后端
- 根/后端/受保护
嗯,第一个 Yii 应用程序直接位于与框架文件夹相同级别的根目录中。第二个 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 文件路径都搞砸了:例如。位于 backend/css 内的 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;
}
}
【问题讨论】:
-
你为什么不把它当作一个 Yii 模块来使用呢?你可以创建多个子应用,因为 Yii 方便你制作,然后你可以访问它看起来像 www.domain.com/backend/controller/action