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

how to remove url (/web/index.php) yii 2 and set route with parameter with clean url?(如何删除 url (/web/index.php) yii 2 并使用干净的 url 参数设置路由?)
本文介绍了如何删除 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 参数设置路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)