Yii2隱藏frontend/web和backend/web的方法
Yii 是一個(gè)高性能,基于組件的 PHP 框架,用于快速開(kāi)發(fā)現(xiàn)代 Web 應(yīng)用程序。名字 Yii (讀作 `易`)在中文里有 “極致簡(jiǎn)單與不斷演變” 兩重含義,也可看作 **Yes It Is**! 的縮寫(xiě)。
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
以上是高級(jí)版的Advanced配置方法,基礎(chǔ)版的不需要這樣配置。
Advanced和 basic 最大的區(qū)別就是分離了前后臺(tái) 分別是 backend目錄和frontend目錄 這兩個(gè)目錄實(shí)際相對(duì)于 basic 來(lái)說(shuō)其實(shí)就是兩個(gè)Yii應(yīng)用 他們公用的比如Model部分都存放在Common目錄 這種高級(jí)應(yīng)用適用于比較復(fù)雜大型的項(xiàng)目用于徹底分離開(kāi)前后臺(tái)業(yè)務(wù)邏輯 因此訪問(wèn)前后臺(tái)就相當(dāng)于訪問(wèn)兩個(gè)不同的應(yīng)用
因此在配置Vhost webroot 目錄的時(shí)候 假設(shè)域名為 www.xxx.com 那么 www.xxx.com指向前臺(tái)目錄 /frontend/web/
配置二級(jí)域名root.xxx.com 指向/backend/web/
以上所述是小編給大家分享的Yii2隱藏frontend/web和backend/web的方法,希望大家喜歡。
- Yii的CDbCriteria查詢條件用法實(shí)例
- Yii使用find findAll查找出指定字段的實(shí)現(xiàn)方法
- Yii操作數(shù)據(jù)庫(kù)的3種方法
- Yii框架中 find findAll 查找出制定的字段的方法對(duì)比
- Yii調(diào)試SQL的常用方法
- Yii2創(chuàng)建表單(ActiveForm)方法詳解
- PHP 基于Yii框架中使用smarty模板的方法詳解
- Yii實(shí)現(xiàn)多數(shù)據(jù)庫(kù)主從讀寫(xiě)分離的方法
- Yii中Model(模型)的創(chuàng)建及使用方法
- Yii使用ajax驗(yàn)證顯示錯(cuò)誤messagebox的解決方法
- PHP的Yii框架中創(chuàng)建視圖和渲染視圖的方法詳解
- yii實(shí)現(xiàn)model添加默認(rèn)值的方法(2種方法)
- Yii CDBCriteria常用方法實(shí)例小結(jié)
相關(guān)文章
tp5(thinkPHP5)框架連接數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了tp5(thinkPHP5)框架連接數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了基于thinkPHP5框架連接數(shù)據(jù)庫(kù)的相關(guān)配置、數(shù)據(jù)讀取、模板渲染等操作技巧,需要的朋友可以參考下2018-12-12
ThinkPHP實(shí)現(xiàn)動(dòng)態(tài)包含文件的方法
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)動(dòng)態(tài)包含文件的方法,是進(jìn)行ThinkPHP項(xiàng)目開(kāi)發(fā)中非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
php學(xué)習(xí)Eloquent修改器源碼示例解析
這篇文章主要為大家介紹了php學(xué)習(xí)Eloquent修改器源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
微信小程序之支付后調(diào)用SDK的異步通知及驗(yàn)證處理訂單方法
下面小編就為大家分享一篇微信小程序之支付后調(diào)用SDK的異步通知及驗(yàn)證處理訂單方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2018-01-01
Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法
這篇文章主要介紹了Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級(jí)聯(lián)動(dòng)功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了dropdownlist下拉列表實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)調(diào)用的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-07-07
PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
短網(wǎng)址(Short URL)顧名思義就是在形式上比較短的網(wǎng)址。在Web 2.0的今天,不得不說(shuō)這是一個(gè)潮流。目前已經(jīng)有許多類似服務(wù),借助短網(wǎng)址您可以用簡(jiǎn)短的網(wǎng)址替代原來(lái)冗長(zhǎng)的網(wǎng)址,讓使用者可以更容易的分享鏈接,下面來(lái)看看如何用PHP實(shí)現(xiàn)這個(gè)功能,有需要的朋友們可以參考。2016-09-09
Yii中的relations數(shù)據(jù)關(guān)聯(lián)查詢及統(tǒng)計(jì)功能用法詳解
這篇文章主要介紹了Yii中的relations數(shù)據(jù)關(guān)聯(lián)查詢及統(tǒng)計(jì)功能用法,結(jié)合實(shí)例形式分析了關(guān)聯(lián)查詢命名空間及評(píng)論統(tǒng)計(jì)功能相關(guān)技巧,需要的朋友可以參考下2016-07-07
PHP 數(shù)據(jù)結(jié)構(gòu)隊(duì)列(SplQueue)和優(yōu)先隊(duì)列(SplPriorityQueue)簡(jiǎn)單使用實(shí)例
這篇文章主要介紹了PHP 數(shù)據(jù)結(jié)構(gòu)隊(duì)列(SplQueue)和優(yōu)先隊(duì)列(SplPriorityQueue)簡(jiǎn)單使用實(shí)例,需要的朋友可以參考下2015-05-05

