Yii的:的.htaccess和urlManager单独的后端和前端 [英] Yii: .htaccess and urlManager for separate backend and frontend

查看:243
本文介绍了Yii的:的.htaccess和urlManager单独的后端和前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  /资产
/后端
   /控制器
   /配置
      main.php
   /楷模
   /人次
/共同
   /楷模
/保护
   /控制器
   /配置
      main.php
   /楷模
   /人次
的.htaccess
backend.php
的index.php
 

解决方法: @ bool.dev一切它的工作有极大的帮助后,让我加入这里的每一个需要的最终文件。在前端我使用的路径格式的URL和隐藏的index.php

/backend/config/main.php

  $后台=目录名(目录名(__ FILE__));
Yii的:: setPathOfAlias​​('后台',$后端);
返回数组(
basePath'=> $后端,

controllerPath'=> $后端。'/控制器,
viewPath'=> $后端。'/意见,
的'runtimepath'=> $后端。/运行,

...);
 

/protected/config/main.php

 'urlManager'=>阵列(
    urlFormat'=>'路',
    showScriptName'=>假的,
    规则=>阵列(
            '<控制器:\ w +> /< ID:\ D +>'=>'<控制器> /观点,
            '<控制器:\ w +> /<作用:\ w +> /< ID:\ D +>'=>'<控制器> /<作用>',
            '<控制器:\ w +> /<作用:\ w +>'=>'<控制器> /<作用>',
    ),
),
 

的.htaccess

 选项+了FollowSymLinks
IndexIgnore * / *
< IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /警予/例子/
重写规则后端后端\ .PHP [T =应用程序/ X的httpd  -  PHP的]

#如果一个目录或文件是否存在,直接使用它
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d

#否则将其转发到index.php
重写规则。的index.php
< / IfModule>
 

backend.php

  $的Yii =目录名(__ FILE __)'/ .. / .. /警予/框架/ yii.php。
$配置=目录名(__ FILE __)'/后端/配置/ main.php。
require_once($ Yii的);
Yii的:: setPathOfAlias​​('普通',目录名(__ FILE __)DIRECTORY_SEPARATOR.'common');
Yii的:: createWebApplication($配置) - >运行();
 

的index.php

  $的Yii =目录名(__ FILE __)'/ .. / .. /警予/框架/ yii.php。
$配置=目录名(__ FILE __)'/保护/配置/ main.php。
require_once($ Yii的);
Yii的:: setPathOfAlias​​('普通',目录名(__ FILE __)DIRECTORY_SEPARATOR.'common');
Yii的:: createWebApplication($配置) - >运行();
 

解决方案

根据由强这个wiki文章,你可以做如下修改,它应该工作:

  // backend.php:
要求('路径/到/ yii.php');
Yii的:: createWebApplication(后台/配置/ main.php') - >运行();
 

然后在后端的配置(即后台/配置/ main.php 的):

  $后台=目录名(目录名(__ FILE__));
$前端=目录名($后端);
Yii的:: setPathOfAlias​​('后台',$后端);

返回数组(
    basePath'=> $后端,

    controllerPath'=> $后端。'/控制器,
    viewPath'=> $后端。'/意见,
    的'runtimepath'=> $后端。/运行,

    '进口'=>阵列(
        'backend.models。*',
    ),
    // ...其他配置...
);
 

不过对于这个工作,我们需要主要的.htaccess路由example.com/backend到backend.php,我还没有想通了呢。

编辑:
只要想通了:

  RewriteEngine叙述上
的RewriteBase / projectroot /
重写规则后端后端\ .PHP [T =应用程序/ X的httpd  -  PHP的]
 

的RewriteBase 对我来说是重要的,因为 backend.php 的没有被发现时,我没有给正确的 projectroot 的,基本上应该是,你有入口脚本的目录中的 backend.php 的。

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

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=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();

解决方案

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();

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 ...
);

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.

Edit:
Just figured out:

RewriteEngine On
RewriteBase /projectroot/
RewriteRule backend backend\.php [T=application/x-httpd-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆