프런트엔드/웹 및 백엔드/웹을 숨기는 Yii2 방법

高洛峰
풀어 주다: 2023-03-05 08:04:01
원래의
1041명이 탐색했습니다.

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

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&#39;t have this function, the admin site will 404 if you leave off
    the trailing slash.
    E.g.:
    Wouldn&#39;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
&#39;request&#39;=>[
  &#39;class&#39; => &#39;common\components\Request&#39;,
  &#39;web&#39;=> &#39;/frontend/web&#39;
],
&#39;urlManager&#39; => [
    &#39;enablePrettyUrl&#39; => true,
    &#39;showScriptName&#39; => false,
],
// backend, under components array
&#39;request&#39;=>[
  &#39;class&#39; => &#39;common\components\Request&#39;,
  &#39;web&#39;=> &#39;/backend/web&#39;,
  &#39;adminUrl&#39; => &#39;/admin&#39;
],
&#39;urlManager&#39; => [
    &#39;enablePrettyUrl&#39; => true,
    &#39;showScriptName&#39; => 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
로그인 후 복사

以上是高级版的Advanced配置方法,基础版的不需要这样配置。

Advanced和 basic 最大的区别就是分离了前后台 分别是 backend目录和frontend目录 这两个目录实际相对于 basic 来说其实就是两个Yii应用 他们公用的比如Model部分都存放在Common目录 这种高级应用适用于比较复杂大型的项目用于彻底分离开前后台业务逻辑 因此访问前后台就相当于访问两个不同的应用
因此在配置Vhost webroot 目录的时候 假设域名为 www.xxx.com 那么 www.xxx.com指向前台目录 /frontend/web/
配置二级域名root.xxx.com 指向/backend/web/

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

更多Yii2隐藏frontend/web和backend/web的方法相关文章请关注PHP中文网!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!