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

标签 php .htaccess yii2 clean-urls 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

这是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>',
            ),
        ],

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

第二个问题:

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

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

最佳答案

对于高级应用,请遵循以下步骤:

1) 将以下htaccess添加到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) 将以下htaccess添加到安装应用程序的root 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

3) 编辑您的 frontend/config/main.php 文件,在顶部添加以下内容

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

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

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

就是这样,现在你可以在没有 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',
           ),
],

关于php - 如何删除 url (/web/index.php) yii 2 并使用干净的 url 参数设置路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29885970/

相关文章:

php - 粉丝页面的长期访问 token 。管理页面权限

apache - 规范 URL 和 URL 重写

.htaccess - GET-Request 中的斜杠 (/) 和我在 htaccess 中的 mod_rewrite 语句

yii2 - 如何在Yii2中启用 Debug模式?

php - 在 PHPExcel 中设置自动高度不起作用

php - Mongodb - 数组查询,仅查找所有元素匹配的位置

php - 另一个password_verify()问题可能与数据库相关

php - 消息 : file_get_contents(sso. json):无法打开流:没有这样的文件或目录

apache - .htaccess 删除 .html 并强制重定向到无扩展名的 url

php - 如何在 Yii2 项目中添加 PHP 请求