1

urlManager 構成を使用して、次のようなきれいな URL を取得しました。

   'urlManager' => [
                'class' => 'yii\web\UrlManager',
                'enablePrettyUrl' => true,
                'showScriptName' => false,
                'enableStrictParsing' => false,
                'rules' => [
                    'login/' => 'site/login',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>/',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>/',
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                ],
            ],

私のローカルホストでは機能していますが、サーバーでは機能していません。.htaccess ファイルの内容:

# Increase cookie security
<IfModule php5_module>
  php_value session.cookie_httponly true
</IfModule>

# Settings to hide index.php and ensure pretty urls
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php
4

2 に答える 2

1

YII2 Advance テンプレートでプリティ URL を有効にします。

最初に、プロジェクト ディレクトリのルートに .htaccess ファイルを作成します。プロジェクト ディレクトリ名が「Yii-Advance」の場合と同様に、ファイル Yii-Advance/.htaccess を作成し、そこに次のコードを配置します。

ステップ1

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>

    <IfModule mod_rewrite.c>
    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
</IfModule>

ステップ2

「Request.php」という名前の新しいファイルを作成し、このコードで「common/components/」の下に置きます。

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

ステップ 3

これらの行を frontend/config/main.php の下に置きます

  'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/frontend/web'
],

ステップ 4

これらの行を backend/config/main.php の下に置きます

  'request'=>[
    'class' => 'common\components\Request',
    'web'=> '/backend/web',
    'adminUrl' => '/admin'
],

ステップ 6

このように、Frontend/config/main.php と backend/config/main.php でプリティ URL を有効にします。私の場合、「サイト、ユーザー」という 2 つのコントローラーがあります。

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<alias:index|login|logout|contact|about|signup|request-password-reset|reset-password>' => 'site/<alias>',
            '<alias:index|create|confirm|confirmation|update|delete>' => 'user/<alias>',
        ],
    ],

それでおしまい

yii2 アドバンス テンプレート アプリケーションにアクセスすると、このように URL が表示されます。

これらのステップの前の URL は次のとおりです: http://localhost/Yii-Advance/frontend/web/site/login

これらの手順の後、URL は次のようになります: http://localhost/Yii-Advance/login

于 2015-04-22T13:53:15.450 に答える