欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Yii2隐藏frontend/web和backend/web的方法

程序员文章站 2022-06-02 23:16:51
yii 是一个高性能,基于组件的 php 框架,用于快速开发现代 web 应用程序。名字 yii (读作 `易`)在中文里有 “极致简单与不断演变” 两重含义,也可看作 *...

yii 是一个高性能,基于组件的 php 框架,用于快速开发现代 web 应用程序。名字 yii (读作 `易`)在中文里有 “极致简单与不断演变” 两重含义,也可看作 **yes it is**! 的缩写。

create .htaccess file in root folder, i.e advanced/.htaccess and write below code.

options +followsymlinks
rewriteengine on
# deal with admin first
rewritecond %{request_uri} ^/(admin) <------
rewriterule ^admin/assets/(.*)$ backend/web/assets/$1 [l]
rewriterule ^admin/css/(.*)$ backend/web/css/$1 [l]
rewritecond %{request_uri} !^/backend/web/(assets|css)/ <------
rewritecond %{request_uri} ^/(admin) <------
rewriterule ^.*$ backend/web/index.php [l]
rewritecond %{request_uri} ^/(assets|css) <------
rewriterule ^assets/(.*)$ frontend/web/assets/$1 [l]
rewriterule ^css/(.*)$ frontend/web/css/$1 [l]
rewritecond %{request_uri} !^/(frontend|backend)/web/(assets|css)/ <------
rewritecond %{request_uri} !index.php
rewritecond %{request_filename} !-f [or]
rewritecond %{request_filename} !-d
rewriterule ^.*$ frontend/web/index.php 

note : if you are trying in local server then replace ^/ with ^/project_name/ where you see arrow sign. remove those arrow sign <------ after setup is done.
now create a components/request.php file in common directory and write below code in this file.

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;
  }
  /*
    if you don't have this function, the admin site will 404 if you leave off 
    the trailing slash.
    e.g.:
    wouldn't work:
    site.com/admin
    would work:
    site.com/admin/
    using this function, both will work.
  */
  public function resolvepathinfo(){
    if($this->geturl() === $this->adminurl){
      return "";
    }else{
      return parent::resolvepathinfo();
    }
  }
} 

installing component. write below code in frontend/config/main.php and backend/config/main.phpfiles respectively.

//frontend, under components array
'request'=>[
  'class' => 'common\components\request',
  'web'=> '/frontend/web'
],
'urlmanager' => [
    'enableprettyurl' => true,
    'showscriptname' => false,
],
// backend, under components array
'request'=>[
  'class' => 'common\components\request',
  'web'=> '/backend/web',
  'adminurl' => '/admin'
],
'urlmanager' => [
    'enableprettyurl' => true,
    'showscriptname' => false,
], 

create .htaccess file in web directory

rewriteengine on 
rewritecond %{request_filename} !-f 
rewritecond %{request_filename} !-d 
rewriterule ^(.*)$ /index.php?/$1 [l] 

note: make sure you have enabled your mod rewrite in apache
thats it! you can try your project with

www.project.com/admin, www.project.com 

in local server

localhost/project_name/admin, localhost/project_name 

以上是高级版的advanced配置方法,基础版的不需要这样配置。

advanced和 basic 最大的区别就是分离了前后台 分别是 backend目录和frontend目录 这两个目录实际相对于 basic 来说其实就是两个yii应用 他们公用的比如model部分都存放在common目录 这种高级应用适用于比较复杂大型的项目用于彻底分离开前后台业务逻辑 因此访问前后台就相当于访问两个不同的应用
因此在配置vhost webroot 目录的时候 假设域名为 那么 指向前台目录 /frontend/web/
配置二级域名root.xxx.com 指向/backend/web/

以上所述是小编给大家分享的yii2隐藏frontend/web和backend/web的方法,希望大家喜欢。