Yii:.htaccess和urlManager用于单独的后端和前端

问题描述 投票:6回答:1

我有一个困难时期来配置我的.htaccess和urlManager在Yii的项目有在前端http://www.example.com和后端的http://www.example.com/backend与以下文件夹结构。 欢迎任何帮助。 谢谢。

/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();
.htaccess yii frontend backend
1个回答
6
投票

根据Qiang的这篇wiki文章 ,您可以进行以下更改,它应该可以正常工作:

// backend.php:
require('path/to/yii.php');
Yii::createWebApplication('backend/config/main.php')->run();

然后在你的后端的配置(即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,我还没想到。

编辑:
刚想通了:

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

RewriteBase对我很重要,因为当我没有给出正确的projectroot时找不到backend.php ,基本上它应该是你有入口脚本backend.php的目录。

© www.soinside.com 2019 - 2024. All rights reserved.