Yii:.htaccess 和 urlManager 用于单独的后端和前端 [英] Yii: .htaccess and urlManager for separate backend and frontend
问题描述
我很难在 Yii 项目中配置我的 .htaccess 和 urlManager 以在 http://www.example 中有前端.com 和 http://www.example.com/backend 中的后端,具有以下文件夹结构.欢迎任何帮助.谢谢.
I'm having a hard time to configure my .htaccess and the urlManager in a Yii project to have the frontend in http://www.example.com and the backend in http://www.example.com/backend with the following folder structure. Any help is welcome. Thanks.
/assets
/backend
/controllers
/config
main.php
/models
/views
/common
/models
/protected
/controllers
/config
main.php
/models
/views
.htaccess
backend.php
index.php
解决方案: 在@bool.dev 的大力帮助下,一切正常,所以我在这里添加了所有需要的最终文件.在前端,我使用 url 的路径格式并隐藏 index.php
Solution: after the great help of @bool.dev everything it's working, so I'm adding here every needed final file. In the frontend I'm using path format for the url and hiding the index.php
/backend/config/main.php
/backend/config/main.php
$backend=dirname(dirname(__FILE__));
Yii::setPathOfAlias('backend', $backend);
return array(
'basePath' => $backend,
'controllerPath' => $backend.'/controllers',
'viewPath' => $backend.'/views',
'runtimePath' => $backend.'/runtime',
...);
/protected/config/main.php
/protected/config/main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:w+>/<id:d+>'=>'<controller>/view',
'<controller:w+>/<action:w+>/<id:d+>'=>'<controller>/<action>',
'<controller:w+>/<action:w+>'=>'<controller>/<action>',
),
),
.htaccess
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /yii/example/
RewriteRule backend backend.php [T=application/x-httpd-php]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
后端.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/backend/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();
index.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();
推荐答案
根据 这篇 wiki 文章 作者:qiang,您可以进行以下更改,它应该可以工作:
According to this wiki article by Qiang, you could make the following changes and it should work:
// backend.php:
require('path/to/yii.php');
Yii::createWebApplication('backend/config/main.php')->run();
然后在您的后端配置(即backend/config/main.php)中:
Then in your backend's config (i.e backend/config/main.php):
$backend=dirname(dirname(__FILE__));
$frontend=dirname($backend);
Yii::setPathOfAlias('backend', $backend);
return array(
'basePath' => $backend,
'controllerPath' => $backend.'/controllers',
'viewPath' => $backend.'/views',
'runtimePath' => $backend.'/runtime',
'import' => array(
'backend.models.*',
),
// ... other configurations ...
);
但是为了让这个工作,我们需要主 .htaccess 将 example.com/backend 路由到 backend.php,我还没有弄清楚.
But for this to work we need the main .htaccess to route example.com/backend to backend.php, which i haven't figured out yet.
刚刚发现:
RewriteEngine On
RewriteBase /projectroot/
RewriteRule backend backend.php [T=application/x-httpd-php]
RewriteBase
对我来说很重要,因为当我没有给出正确的 projectroot 时,backend.php 就找不到了,基本上它应该是您拥有入口脚本的目录backend.php.
The RewriteBase
was important for me, as the backend.php was not found when i hadn't given the correct projectroot, basically it should be the directory where you have the entry script backend.php.
这篇关于Yii:.htaccess 和 urlManager 用于单独的后端和前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!