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)文章
Laravel + Elasticsearch 實(shí)現(xiàn)中文搜索的方法
這篇文章主要介紹了Laravel + Elasticsearch 實(shí)現(xiàn)中文搜索的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02PHP stream_context_create()函數(shù)的使用示例
這篇文章主要介紹了PHP stream_context_create()函數(shù)的使用示例,stream_context_create()函數(shù)是用來(lái) 創(chuàng)建打開(kāi)文件的上下文件選項(xiàng),用于fopen(),file_get_contents()等過(guò)程的超時(shí)設(shè)置、代理服務(wù)器、請(qǐng)求方式、頭信息設(shè)置的特殊過(guò)程,需要的朋友可以參考下2015-05-05thinkPHP5使用laypage分頁(yè)插件實(shí)現(xiàn)列表分頁(yè)功能
這篇文章主要為大家詳細(xì)介紹了thinkPHP5使用laypage分頁(yè)插件實(shí)現(xiàn)列表分頁(yè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11PHP會(huì)員找回密碼功能的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇PHP會(huì)員找回密碼功能的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09