加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 02:59:26 所属栏目:PHP教程 来源:网络整理
导读:《PHP实战:Yii2隐藏frontend/web和backend/web的方法》要点: 本文介绍了PHP实战:Yii2隐藏frontend/web和backend/web的方法,希望对您有用。如果有疑问,可以联系我们。 PHP实例 Yii 是一个高性能,基于组件的 PHP 框架,用于快速开发现代 Web 应用程序.名字

《PHP实战:Yii2隐藏frontend/web和backend/web的方法》要点:
本文介绍了PHP实战:Yii2隐藏frontend/web和backend/web的方法,希望对您有用。如果有疑问,可以联系我们。

PHP实例Yii 是一个高性能,基于组件的 PHP 框架,用于快速开发现代 Web 应用程序.名字 Yii (读作 `易`)在中文里有 “极致简单与不断演变” 两重含义,也可看作 **Yes It Is**! 的缩写.

PHP实例Create .htaccess file in root folder,i.e advanced/.htaccess and write below code.

PHP实例
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 

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.

PHP实例
namespace commoncomponents;
class Request extends yiiwebRequest {
  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();
    }
  }
} 

PHP实例Installing component. Write below code in frontend/config/main.php and backend/config/main.phpfiles respectively.

PHP实例
//frontend,under components array
'request'=>[
  'class' => 'commoncomponentsRequest','web'=> '/frontend/web'
],'urlManager' => [
    'enablePrettyUrl' => true,'showScriptName' => false,],// backend,'web'=> '/backend/web','adminUrl' => '/admin'
],

PHP实例create .htaccess file in web directory

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

PHP实例Note: make sure you have enabled your mod rewrite in apache
Thats it! You can try your project with

PHP实例
www.project.com/admin,www.project.com 

PHP实例in local server

PHP实例
localhost/project_name/admin,localhost/project_name 

PHP实例以上是高级版的Advanced配置方法,基础版的不需要这样配置.

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

PHP实例以上所述是小编给大家分享的Yii2暗藏frontend/web和backend/web的方法,希望大家喜欢.

编程之家培训学院每天发布《PHP实战:Yii2隐藏frontend/web和backend/web的方法》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读