程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> Yii2隱藏frontend/web和backend/web的方法

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

編輯:PHP綜合

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的方法,希望大家喜歡。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved