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

2023-10-16php开发问题
13

本文介绍了如何删除 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',
           ),
],

这篇关于如何删除 url (/web/index.php) yii 2 并使用干净的 url 参数设置路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17