Yii2隐藏frontend/web和backend/web的方法
Yii是一个高性能,基于组件的PHP框架,用于快速开发现代Web应用程序。名字Yii(读作`易`)在中文里有“极致简单与不断演变”两重含义,也可看作**YesItIs**!的缩写。
Create.htaccessfileinrootfolder,i.eadvanced/.htaccessandwritebelowcode.
Options+FollowSymlinks RewriteEngineOn #dealwithadminfirst 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:ifyouaretryinginlocalserverthenreplace^/with^/project_name/whereyouseearrowsign.Removethosearrowsign<------aftersetupisdone.
Nowcreateacomponents/Request.phpfileincommondirectoryandwritebelowcodeinthisfile.
namespacecommon\components; classRequestextends\yii\web\Request{ public$web; public$adminUrl; publicfunctiongetBaseUrl(){ returnstr_replace($this->web,"",parent::getBaseUrl()).$this->adminUrl; } /* Ifyoudon'thavethisfunction,theadminsitewill404ifyouleaveoff thetrailingslash. E.g.: Wouldn'twork: site.com/admin Wouldwork: site.com/admin/ Usingthisfunction,bothwillwork. */ publicfunctionresolvePathInfo(){ if($this->getUrl()===$this->adminUrl){ return""; }else{ returnparent::resolvePathInfo(); } } }
Installingcomponent.Writebelowcodeinfrontend/config/main.phpandbackend/config/main.phpfilesrespectively.
//frontend,undercomponentsarray 'request'=>[ 'class'=>'common\components\Request', 'web'=>'/frontend/web' ], 'urlManager'=>[ 'enablePrettyUrl'=>true, 'showScriptName'=>false, ], //backend,undercomponentsarray 'request'=>[ 'class'=>'common\components\Request', 'web'=>'/backend/web', 'adminUrl'=>'/admin' ], 'urlManager'=>[ 'enablePrettyUrl'=>true, 'showScriptName'=>false, ],
create.htaccessfileinwebdirectory
RewriteEngineOn RewriteCond%{REQUEST_FILENAME}!-f RewriteCond%{REQUEST_FILENAME}!-d RewriteRule^(.*)$/index.php?/$1[L]
Note:makesureyouhaveenabledyourmodrewriteinapache
Thatsit!Youcantryyourprojectwith
www.project.com/admin,www.project.com
inlocalserver
localhost/project_name/admin,localhost/project_name
以上是高级版的Advanced配置方法,基础版的不需要这样配置。
Advanced和basic最大的区别就是分离了前后台分别是backend目录和frontend目录这两个目录实际相对于basic来说其实就是两个Yii应用他们公用的比如Model部分都存放在Common目录这种高级应用适用于比较复杂大型的项目用于彻底分离开前后台业务逻辑因此访问前后台就相当于访问两个不同的应用
因此在配置Vhostwebroot目录的时候假设域名为www.xxx.com那么www.xxx.com指向前台目录/frontend/web/
配置二级域名root.xxx.com指向/backend/web/
以上所述是小编给大家分享的Yii2隐藏frontend/web和backend/web的方法,希望大家喜欢。