【问题标题】:How can I add a query string to a route in php?如何将查询字符串添加到 php 中的路由?
【发布时间】:2016-05-26 23:59:28
【问题描述】:

我正在尝试将默认主页路由默认设置为特定类别。

在我的 routes.php 文件中,我已将发布新操作重新路由到主页,因为我希望发布广告表单成为主页,但在尝试添加查询字符串时我一直遇到错误“ ?category=wanted-ads"

我的目标是我希望它默认为表单选项的一部分 /publish-new.html?category=wanted-ads 页面。

对于一个简单的问题,我很抱歉,我是 php 和 kohana 框架的新手,我正在使用开放分类脚本来创建分类网站。

提前致谢。

 URL::title(__('publish new'))

Route::set('post_new', URL::title(__('publish new')).'.html')
->defaults(array(
        'controller' => 'new',    
        'action'     => 'index',
));

【问题讨论】:

    标签: php routing kohana-3


    【解决方案1】:

    您是否尝试过手动获取查询字符串参数 ($_GET)?如果是,显示什么?

    通过快速阅读 Kohana 文档,我发现了一些可以帮助您的东西。相反,您可以通过查询字符串传递所需的类别,您可以通过参数传递。

    Route::set('post_new', '/post_new/category/:<category>', array('category' => '.*'))
        ->defaults(array(
            'controller' => 'new',
            'action' => 'index',
    ));
    

    要获取此参数,您只需执行以下操作:

    // From within a controller:
    $this->request->param('category');
    
    // Can be used anywhere:
    Request::current()->param('category');
    

    这在许多其他框架中运行良好,在 Kohana 中也可能运行良好。

    来源:https://kohanaframework.org/3.2/guide/kohana/routing#examples

    【讨论】:

      猜你喜欢
      • 2015-05-20
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2015-04-12
      相关资源
      最近更新 更多