温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Yii2怎么隐藏frontend/web和backend/web

发布时间:2021-08-06 23:59:16 来源:亿速云 阅读:196 作者:chen 栏目:开发技术

本篇内容主要讲解“Yii2怎么隐藏frontend/web和backend/web”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Yii2怎么隐藏frontend/web和backend/web”吧!

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 目录的时候 假设域名为 www.xxx.com 那么 www.xxx.com指向前台目录 /frontend/web/
配置二级域名root.xxx.com 指向/backend/web/

到此,相信大家对“Yii2怎么隐藏frontend/web和backend/web”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI