簡體   English   中英

如何在yii2中重寫URL

[英]How to rewrite the url in yii2

我已經在系統上安裝了yii2。 默認網址是localhost/projectname/backend/web/

我希望前端的URL為http://localhost/projectname ,后端為http://localhost/projectname/admin 我正在按照第二個答案,即由該鏈接的despotbg給出Yii2 htaccess-如何完全隱藏前端/網絡和后端/網絡,但出現以下錯誤

無效的呼叫– yii \\ base \\ InvalidCallException設置只讀屬性:yii \\ web \\ Application :: request

請建議我如何刪除此錯誤,以便我可以重寫我的項目URL。 我在wamp服務器上工作

答案已更新,現在工作正常...

前端

修改文件frontend / config / main.php:

....
    'components' => [
        ....
        'request'=>[
            'baseUrl'=>'/projectname',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/projectname/index.php',
        ],
        // use the following, if you want to enable speaking URL for the frontend
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//        ],
    ],

后端

修改文件backend / config / main.php:

....
    'components' => [
        ....
        'request'=>[
            'baseUrl'=>'/projectname/admin',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/projectname/admin/index.php',
        ],
        // use the following, if you want to enable speaking URL for the backend
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//        ],
    ],

阿帕奇(.htaccess與mod_rewrite)

在項目根目錄(composer.json在其中)中創建文件.htaccess:

RewriteEngine On

# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend)/web/ - [L]

# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^admin(/(.*))?$ backend/web/$2 [S=1]

# handle the case of frontend
RewriteRule .* frontend/web/$0

# Uncomment the following, if you want speaking URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/web)/.*$ $1/index.php

資源

后端\\ CONFIG \\ main.php

// backend, under components array
         'request'=>[
             'class' => 'common\components\Request',
             'web'=> '/backend/web',
             'adminUrl' => '/admin'
         ],
         'urlManager' => [
                 'enablePrettyUrl' => true,
                 'showScriptName' => false,
         ],

在common \\ components上創建request.php文件

<?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();
        }
    }
}

試試這個,希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM