本文介绍了如何创建使用Yiinitializr Yii的项目友情链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Yiinitializr网站的结构我的Yii项目。其结构是这样的:

 根
--backend
----非标准Yii的文件夹
----...
 -  - 万维网
------ index.php文件(admin.mysite.com)
 - 共同
后端和前端----共同文件夹
----...
--backend
----非标准Yii的文件夹
----...
 -  - 万维网
------ index.php文件(mysite.com)
 

空白Yiinitializr strucure看到在github

现在的问题是如何使工作的网址是这样的:

  admin.mysite.com/invites~~V  - 在后台
 

  mysite.com/users  - 在前端
 

与路由故障出现时,我上传项目的虚拟主机。后端例如:

  admin.mysite.com/backend/www/?r=site/invites  - 正常工作
admin.mysite.com/backend/www/invites  - 工作正常
admin.mysite.com/?r=site/invites  - 工作正常
admin.mysite.com/invites  - 重定向到指数
 

根文件夹有自己的.htaccess与此内容:

  RewriteEngine叙述上
重写规则^后端/ WWW /  -  [最后]
的RewriteCond%{HTTP_HOST} admin.mysite.com [NOCASE]
重写规则(。*)后端/ WWW / $ 1 [最后]

重写规则^前端/ WWW /  -  [最后]
的RewriteCond%{HTTP_HOST}!admin.mysite.com [NOCASE]
重写规则(。*)的前端/ WWW / $ 1 [最后]
 

后端/网络/的.htaccess有重写规则:

 < IfModule mod_rewrite.c>
  选项​​+的FollowSymLinks
  RewriteEngine叙述上
    的RewriteCond%{} REQUEST_FILENAME!-f
    的RewriteCond%{} REQUEST_FILENAME!-d
    重写规则。的index.php
< / IfModule>
 

和Yii的urlManagers code:

 'urlManager'=>阵列(
    //取消注释如果您启用了Apache的重写模块以下。
    urlFormat'=> '路径',
    showScriptName'=>假,

    规则=>阵列(
        //默认规则
        '<网页:\ w +> => 网站/<网页>',
        ''=> 网站/索引,
        '<控制器:\ w +> /< ID:\ D +> => '<控制器> /观点,
        '<控制器:\ w +> /<作用:\ w +> /< ID:\ D +> => '<控制器> /<作用>',
        '<控制器:\ w +> /<作用:\ w +> => '<控制器> /<作用>',
    ),
),
 

解决方案

之后更新在聊天讨论:
所有的问题将在 $ _ SERVER ['SCRIPT_NAME'] ,它等于 /backend/www/index.php 。这就是为什么请求 CHtt prequest :: getPathInfo 解析为空值,因为$的baseUrl(它是$ _ SERVER ['SCRIPT_NAME']不是<$ C一BASEDIR $ C>)和 $ _ SERVER ['PHP_SELF'] == $ _ SERVER ['SCRIPT_NAME']

两种解决方案:

  1. 设置在年初的index.php $ _ SERVER ['SCRIPT_NAME'] ='的index.php';
  2. 请该子域 admin.example.com 看目录 /后端/ WWW / 直接

I try to use Yiinitializr site structure for my Yii project. The structure looks like this:

root
--backend
----standart yii folders
----...
----www
------index.php (admin.mysite.com)
--common
----common folders for backend and frontend
----...
--backend
----standart yii folders
----...
----www
------index.php (mysite.com)

Blank Yiinitializr strucure see on github

The question is how to make working URLs like this:

admin.mysite.com/invites - in backend

and

mysite.com/users - in frontend

Troubles with routing appeared when i uploaded project on virtual hosting. Backend for example:

admin.mysite.com/backend/www/?r=site/invites - working properly
admin.mysite.com/backend/www/invites - working properly
admin.mysite.com/?r=site/invites - working properly
admin.mysite.com/invites - redirects to index

Root folder has own .htaccess with this content:

RewriteEngine On 
RewriteRule ^backend/www/ - [last] 
RewriteCond %{HTTP_HOST} admin.mysite.com [nocase] 
RewriteRule (.*) backend/www/$1 [last]

RewriteRule ^frontend/www/ - [last] 
RewriteCond %{HTTP_HOST} !admin.mysite.com [nocase] 
RewriteRule (.*) frontend/www/$1 [last]

backend/www/.htaccess have rewrite rules:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php
</IfModule>

And Yii urlManagers code:

'urlManager' => array(
    // uncomment the following if you have enabled Apache's Rewrite module.
    'urlFormat' => 'path',
    'showScriptName' => false,

    'rules' => array(
        // default rules
        '<page:\w+>' => 'site/<page>',
        '' => 'site/index',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
),
解决方案

UPDATED AFTER discussion in the chat:
All problem will be in $_SERVER['SCRIPT_NAME'], it equals /backend/www/index.php. That's why parsing of request in CHttpRequest::getPathInfo is empty value, because $baseUrl (it's a basedir of $_SERVER['SCRIPT_NAME'] not '') and $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME'].

Two solutions:

  1. Set on the beginning of index.php $_SERVER['SCRIPT_NAME'] = '/index.php';
  2. Make that subdomain admin.example.com see to the directory /backend/www/ directly.

这篇关于如何创建使用Yiinitializr Yii的项目友情链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 11:08