《PHP實戰(zhàn):Yii2隱藏frontend/web和backend/web的方法》要點:
本文介紹了PHP實戰(zhàn):Yii2隱藏frontend/web和backend/web的方法,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
PHP實例Yii 是一個高性能,基于組件的 PHP 框架,用于快速開發(fā)現(xiàn)代 Web 應(yīng)用程序.名字 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 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();
}
}
}
PHP實例Installing component. Write below code in frontend/config/main.php and backend/config/main.phpfiles respectively.
PHP實例
//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,
],
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配置方法,基礎(chǔ)版的不需要這樣配置.
PHP實例Advanced和 basic 最大的區(qū)別就是分離了前后臺 分別是 backend目錄和frontend目錄 這兩個目錄實際相對于 basic 來說其實就是兩個Yii應(yīng)用 他們公用的比如Model部分都存放在Common目錄 這種高級應(yīng)用適用于比較復(fù)雜大型的項目用于徹底分離開前后臺業(yè)務(wù)邏輯 因此訪問前后臺就相當(dāng)于訪問兩個不同的應(yīng)用
因此在配置Vhost webroot 目錄的時候 假設(shè)域名為 www.xxx.com 那么 www.xxx.com指向前臺目錄 /frontend/web/
配置二級域名root.xxx.com 指向/backend/web/
PHP實例以上所述是小編給大家分享的Yii2暗藏frontend/web和backend/web的方法,希望大家喜歡.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP實戰(zhàn):Yii2隱藏frontend/web和backend/web的方法》等實戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/8253.html