【问题标题】:Yii2: Pretty URL Rule to allow garbage data in URLYii2:漂亮的 URL 规则以允许 URL 中的垃圾数据
【发布时间】:2016-09-24 23:34:48
【问题描述】:

我在我的项目中使用 URL 重写规则和 URL 管理器。这是我的 URLManager 规则的代码:

        'rules' => [
            '/'=>'site/index',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
            '<module:\w+>/<controller:\w+>/<action:\w+>/<type:(admin|driver|user)>' => '<module>/<controller>/<action>',
            '<module:\w+>/<controller:\w+>/<action:\w+>/<type:(admin|driver|user)>/<id:\d+>' => '<module>/<controller>/<action>',
            '<module:\w+>/<controller:\w+>/<action:\w+>/<type:(admin|driver|user)>/<_:\w+>' => '<module>/<controller>/<action>',
        ],

问题在于 DataTables 在 URL ?_=1474714889055 中发送带有额外参数的数据,这会搞乱一切。我添加了最后一行代码以允许此参数或 Datatable 添加的任何其他垃圾数据。

如何删除此代码或允许它在 URL 中而不影响路由?

问候

编辑:

public function actionVerified()
{
    Yii::$app->response->format = Response::FORMAT_JSON;
    $expression = new \yii\db\Expression('CONCAT(`driver_details`.`first_name`, " ", `driver_details`.`last_name`) as `driver_name`');
    $bookings = \app\models\Bookings::find()
    ->select(["bookings.*",
        "TIME_FORMAT(booking_start, '%h:%i %p') as booking_start",
        "TIME_FORMAT(booking_end, '%h:%i %p') as booking_end",
        "users_main.name as verified_by_name",
        "user_details.name as booked_by", 
        "user_details.contact as contact", 
        new \yii\db\Expression("CONCAT('STARWAY-BKNG-',bookings.id) as id_show"),
        $expression
    ])->joinWith('userDetail')
    ->joinWith('driverDetail')
    ->joinWith('usersMain')
    ->where(['verified'=>1, 'canceled'=> 0, 'completed'=>0])->asArray()->all();
    foreach ($bookings as $key => $booking) {
        unset($booking['userDetail']);
        unset($booking['driverDetail']);
        unset($booking['usersMain']);
        $booking_send[] = $booking;
    }
    if(!isset($booking_send)){
        $booking_send = [];
    }
    return $booking_send;
}

【问题讨论】:

  • ?_=1474714889055 应该不会影响任何事情,除非您在操作中依赖于 _。因为这只是一个查询参数,如果您不需要它,您可以完全忽略它。你能告诉我们你的行为在哪里弄乱了你的东西吗??
  • @leninhasda 这是 URL http://project.local/api/booking/verified/admin/?_=1474714889055,我用这个 ?_ 得到 404,如果我删除它,url 可以工作
  • 好的,我试图重现您的问题:制作了一个 api 模块,制作了一个“预订”控制器,在其中制作了一个 verified 方法,上面只有一行 echo 'hello';。然后我在我的配置文件中添加了你的rules(没有列表行),它仍然可以使用像你这样的url:http://localhost/yii2-test/api/booking/verified/admin/?_=147471488‌​9055。你能展示你的verified 方法吗?
  • 我正在使用经过验证的操作代码更新我的答案
  • @leninhasda 你能分享你的 .htaccess 代码吗?

标签: javascript url-rewriting datatables yii2


【解决方案1】:

我想我知道您为什么会收到 404 错误。它是最后一个 '/' 部分,就在 '?_=147471488‌​9055' 之前,它会导致 404。

根据 Yii2 下面这两个 URL 是不同的:

// these two are not same
http://domain.com/controller/action
http://domain.com/controller/action/

如果你只用trailing slash搜索,你会在Yii Github Issues页面上找到几个讨论。

我不知道最后一个 / 来自哪里,所以我检查了DataTables Documentation,但在他们的示例代码或 ajax url 上没有找到任何最后一个“/”:

// their ajax code 
$(document).ready(function() {
    $('#example').DataTable( {
        "ajax": '../ajax/data/arrays.txt'
    } );
} );

// ajax url being called in their example page
https://datatables.net/examples/ajax/data/arrays.txt?_=1474729703286

所以我猜你的源代码中可能有一个类型,它添加了最后一个/。解决这个问题应该可以解决您的问题。

如果不是这样,您可以尝试在配置中使用以下规则:

'rules' => [
    '/'=>'site/index',
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
    '<module:\w+>/<controller:\w+>/<action:\w+>/<type:(admin|driver|user)>' => '<module>/<controller>/<action>',
    '<module:\w+>/<controller:\w+>/<action:\w+>/<type:(admin\/|driver\/|user\/)>' => '<module>/<controller>/<action>', // allow the trailing slash 
    '<module:\w+>/<controller:\w+>/<action:\w+>/<type:(admin|driver|user)>/<id:\d+>' => '<module>/<controller>/<action>',
],

虽然您当前的解决方案有效,但我不推荐

'<module:\w+>/<controller:\w+>/<action:\w+>/<type:(admin|dri‌​ver|user)>/<_:\*?>' => '<module>/<controller>/<action>'

这条规则是因为它不正确。 &lt;_:\*?&gt;part 表示也允许以下 URL:

// .......................................^ these are definitely not valid.
YOUR_DOMAIN/module/controller/action/type/*
YOUR_DOMAIN/module/controller/action/type/?
YOUR_DOMAIN/module/controller/action/type/*?
YOUR_DOMAIN/module/controller/action/type/?*

我希望你明白我的意思。

最后,我只是使用标准的 wordpress,比如 .htaccess 文件。以防万一:

# BEGIN WordPress
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
# END WordPress

【讨论】:

  • 是的,你是对的 t 是斜线,但你知道最有趣的事情吗?它消失了。现在删除我的第三条规则并没有什么不同。 -.-
猜你喜欢
  • 1970-01-01
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 2017-04-16
相关资源
最近更新 更多