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

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

RewriteEngine on# 如果目录或文件存在,直接使用RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# 否则将其转发到 index.php重写规则.索引.php

这是config/web.php

'urlManager' =>['类' =>'yiiwebUrlManager',//禁用 index.php'showScriptName' =>错误的,//禁用 r= 路由'enablePrettyUrl' =>真的,'规则' =>大批('<控制器:w+>/'=>'<控制器>/视图','<控制器:w+>/<动作:w+>/<id:d+>'=>'<控制器>/<动作>','<控制器:w+>/<动作:w+>'=>'<控制器>/<动作>',),],

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

第二个问题:

我不能用那个干净的 url 参数设置路由,我的路由 Url::toRoute(['transaction/getrequestdetail/', 'id' => 1 ]);

路线应该如何?以及如何使用 2 个参数路由?

解决方案

对于高级应用,请按照以下步骤操作:

1) 将以下 htaccess 添加到 frontend/web

RewriteEngine on# 如果目录或文件存在,直接使用请求RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# 否则将请求转发到 index.php重写规则.索引.php

2) 将以下 htaccess 添加到安装应用程序的根文件夹

# 防止目录列表选项 - 索引索引忽略 */*# 跟随符号链接选项 FollowSymlinks重写引擎开启RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]RewriteRule ^(.+)?$ frontend/web/$1

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

使用yiiwebRequest;$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());

4) 将 request component 添加到同一个文件中的 components 数组,即 frontend/config/main.php

'components' =>['请求' =>['baseUrl' =>$baseUrl,],],

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

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

像这样:

'urlManager' =>['baseUrl' =>$baseUrl,'类' =>'yiiwebUrlManager',//禁用 index.php'showScriptName' =>错误的,//禁用 r= 路由'enablePrettyUrl' =>真的,'规则' =>大批('交易/getrequestdetail/'=>'交易/获取请求详细信息',),],

first question: i already remove index.php, but i want remove /web also. this is my .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

and this is config/web.php

'urlManager' => [
            'class' => 'yiiwebUrlManager',
            // 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>',
            ),
        ],

it's working fine, but it's still using /web . is it possible remove /web ?

second question:

i can't set route with parameter with that clean url, my route Url::toRoute(['transaction/getrequestdetail/', 'id' => 1 ]);

how the route should be ? and how with 2 parameter route ?

解决方案

For advanced application follow these steps:

1) Add the following htaccess to frontend/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

2) Add the following htaccess to root folder where application is installed

# prevent directory listings
Options -Indexes
IndexIgnore */*

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

3) Edit your frontend/config/main.php file with the following at the top

use yiiwebRequest;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());

4) Add the request component to the components array in the same file i.e frontend/config/main.php

'components' => [
        'request' => [
            'baseUrl' => $baseUrl,
        ],
],

That's it.Now you can access the frontend without web/index.php

For you second question you need to write the rule for it in your URL manager component.

Something like this:

'urlManager' => [
            'baseUrl' => $baseUrl,
            'class' => 'yiiwebUrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    'transaction/getrequestdetail/<id>' => 'transaction/getrequestdetail',
           ),
],

相关文章