第一个问题:
我已经删除了 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/