0

我的项目中有以下目录:

  • 项目
    • 前端:(这是 angularjs 应用程序)
    • 后端:(这是 yii2 高级应用)
      • 后端:
        • 控制器
        • 楷模
        • 意见
        • web:后端的根(index.php)
      • 前端:
        • 控制器
        • 楷模
        • 意见
        • web:前端的根目录(index.php)

我想通过这个 URL 指向后端(yii2 应用程序):http://localhost/Project/backend,而前端(角度)应该是根,即http://localhost/Project。依此类推,我想api通过这个 URL 调用控制器:http://localhost/Project/backend/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]

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

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

Request.php(项目/后端/公共/组件):

<?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;
        }
    }
}

main.php(项目/后端/后端/配置):

<?php

return [
    'id' => 'app-backend',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'backend\controllers',
    'bootstrap' => ['log'],
    'modules' => [],
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-backend',
            'class' => 'common\components\Request',
            'web'=> '/backend/web',
            'adminUrl' => '/backend'
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'api-create' => 'api/api-create',
                'api' => 'api/api',
                'mobileapi/<action:\w+>' => 'mobileapi/<action>' 
            ],
        ],
    ],
];
4

0 回答 0