如何删除url(/web/index.php)yii 2并使用带有干净url的参数设置路由?

ran*_*yup 4 php .htaccess clean-urls yii2 yii-url-manager

第一个问题:我已经删除了index.php,但我/web也想删除.这是我的.htaccess

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
Run Code Online (Sandbox Code Playgroud)

这是 config/web.php

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ],
Run Code Online (Sandbox Code Playgroud)

它工作正常,但它仍在使用/web.可以删除/web吗?

第二个问题:

我无法使用干净的网址,我的路线设置带参数的路线 Url::toRoute(['transaction/getrequestdetail/', 'id' => 1 ]);

路线应该如何?以及2参数路线如何?

Chi*_*may 18

对于高级应用,请执行以下步骤

1)增加以下htaccessfrontend/web

RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
Run Code Online (Sandbox Code Playgroud)

2)将以下内容添加htaccessroot folder安装应用程序的位置

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
Run Code Online (Sandbox Code Playgroud)

3)frontend/config/main.php使用顶部的以下内容编辑文件

use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
Run Code Online (Sandbox Code Playgroud)

4)request component在同一个文件中添加组件数组即frontend/config/main.php

'components' => [
        'request' => [
            'baseUrl' => $baseUrl,
        ],
],
Run Code Online (Sandbox Code Playgroud)

就是这样.现在你可以在没有web/index.php的情况下访问前端

对于第二个问题,您需要在URL管理器组件中为其编写规则.

像这样的东西:

'urlManager' => [
            'baseUrl' => $baseUrl,
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    'transaction/getrequestdetail/<id>' => 'transaction/getrequestdetail',
           ),
],
Run Code Online (Sandbox Code Playgroud)