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

时间:2021-05-25

Yii 是一个高性能,基于组件的 PHP 框架,用于快速开发现代 Web 应用程序。名字 Yii (读作 `易`)在中文里有 “极致简单与不断演变” 两重含义,也可看作 **Yes It Is**! 的缩写。

Create .htaccess file in root folder, i.e advanced/.htaccess and write below code.

Options +FollowSymlinksRewriteEngine On# deal with admin firstRewriteCond %{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.phpRewriteCond %{REQUEST_FILENAME} !-f [OR]RewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^.*$ 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; } 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

指向/backend/web/

以上所述是小编给大家分享的Yii2隐藏frontend/web和backend/web的方法,希望大家喜欢。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章