2012-05-18 91 views
6

我很難在Yii項目中配置我的.htaccess和urlManager,使用以下文件夾結構在http://www.example.comhttp://www.example.com/backend中有前端。歡迎任何幫助。謝謝。Yii:用於獨立後端和前端的.htaccess和urlManager

/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

/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

'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> 

backend.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(); 
+0

你能告訴我你爲什麼沒做後端模塊? –

+0

我想遵循強描述的Yii的項目現場http://www.yiiframework.com/wiki/155/the-directory-structure-of-the-yii-project-site/的目錄結構。 – Puigcerber

+0

你不應該有一個單獨的htaccess的後端,後端文件夾中?,說讓example.com的外htaccess的,並建立內部後端另一個htaccess的,對於example.com/backend –

回答

6

this wiki article by Qiang,你可以做如下修改,它應該工作:

// backend.php: 
require('path/to/yii.php'); 
Yii::createWebApplication('backend/config/main.php')->run(); 
在後端的配置

然後(即後端/配置/主.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,我還沒有弄清楚。

編輯:
就想通了:

RewriteEngine On 
RewriteBase /projectroot/ 
RewriteRule backend backend\.php [T=application/x-httpd-php] 

RewriteBase對我很重要,因爲backend.php時,我沒有給正確的projectroot沒有被發現,基本上它應該是你的目錄腳本backend.php

+0

讓我知道這是如何解決你的問題 –

+0

目前沒有什麼,該指南是我嘗試遵循的第一個,但沒有辦法讓它工作... – Puigcerber

+0

其實我試着用你的目錄結構,它的工作,那麼你能告訴我確切的錯誤嗎?或者你遇到的問題? –