This article mainly introduces how Yii2 hides frontendweb and backendweb. if you need it, refer to Yii as a high-performance, component-based PHP Framework for rapid development of modern Web applications. The name Yii has two meanings: "Ultimate simplicity and constant evolution" in Chinese. It can also be seen as ** 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; } /* 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 OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ /index.php?/$1 [L]
Note: make sure you have enabled your mod rewrite in apache
Thats it! You can try your project
www.project.com/admin, www.project.com
In local server
localhost/project_name/admin, localhost/project_name
The above is the Advanced Configuration method of the Advanced Edition, which is not required for the basic edition.
The biggest difference between Advanced and basic is that the two directories are separated: the backend directory and the frontend Directory. actually, compared with basic, they are actually two Yii applications. for example, the Model part is stored in the Common directory. this advanced application is suitable for complex and large-scale projects. it is used to completely split the business logic from the front and back ends. Therefore, accessing the front and back ends is equivalent to accessing two different applications.
Therefore, when configuring the Vhost webroot Directory, assume that the domain name is www.xxx.com, then www.xxx.com points to the foreground directory/frontend/web/
Configure level-2 domain name root.xxx.com pointing to/backend/web/
The preceding section describes how Yii2 hides frontend/web and backend/web.
For more information about how to hide frontend/web and backend/web in Yii2, refer to PHP!