yii2路由和htaccess配置

时间:2017-12-28 15:16:37

标签: apache .htaccess mod-rewrite yii2 yii-routing

我的项目中有以下目录:

  • 项目
    • 前端 :(这是angularjs app)
    • 后端 :(这是yii2高级应用)
      • 后端:
        • 控制器
        • 模型
        • 视图
        • web:后端的根(index.php)
      • 前端:
        • 控制器
        • 模型
        • 视图
        • web:前端的根(index.php)

我想通过此网址指向后端(yii2 app):http://localhost/Project/backend,而前端(angular)应该是根,http://localhost/Project。 依此类推,我想通过以下网址呼叫控制器{ "Id":"123", "Product": "test", "Tags":[ { "Name": "name", "Categories": [ { //item 1 }, { //item 2 }, { //item 3 } ] }, { "Name": "name", "Categories": [ { //item 1 }, { //item 2 }, { //item 3 } ] } ] } http://localhost/Project/backend/api

我到目前为止所尝试的内容:

.htaccess(项目):

api

.htaccess(项目/后端):

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/Project
RewriteRule ^.*$ frontend/backend/web/index.php [L]

RewriteCond %{REQUEST_URI} ^/obeauty/(admin)
RewriteRule ^.*$ backend/backend/web/index.php [L]

.htaccess(项目/后端/后端):

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/Project/(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]

Request.php(Project / backend / common / components):

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?/$1 [L]

main.php(Project / backend / backend / config):

<?php

namespace common\components;

class Request extends \yii\web\Request {
    public $web;
    public $adminUrl;

    public function getBaseUrl(){
        return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
    }

    public function resolvePathInfo(){
        if($this->getUrl() === $this->adminUrl){
            return "";
        }else{
            $pathInfo = parent::resolvePathInfo();
            return $pathInfo == 'index.php' || $pathInfo == 'index.php/' ? 'site/index' : $pathInfo;
        }
    }
}

0 个答案:

没有答案