【问题标题】:Codeigniter: How to capture slashes in route and pass to controller?Codeigniter:如何捕获路由中的斜线并传递给控制器​​?
【发布时间】:2013-01-31 21:42:57
【问题描述】:

我正在尝试从 url 中捕获斜杠并将它们传递到我的控制器中。这是我的问题的一个例子:

假设我 GET http://localhost/here/is/an/example 我的路线如下所示:

$routes['here/is/(:any)'] = 'my_controller/$1';

我在my_controller 中有以下函数定义:

public function index($rest_of_the_path) {
...
}

我希望$rest_of_the_path 的值为'an/example',但它实际上等于'an'如何构造我的代码以便得到我想要的?

P.S.:我使用的是 Codeigniter 2.1.3

【问题讨论】:

    标签: php codeigniter routing routes


    【解决方案1】:

    你可以这样做:

    public function index() {
        $rest_of_the_path = implode("/", func_get_args());  
        ...
    }
    

    【讨论】:

    • 这太棒了!你能向我解释一下它是如何工作的吗?我不明白在没有任何参数的情况下如何调用 func_get_args()。
    • 这就是 func_get_args() 正在做的事情。它是 php 中的一个函数。有关更多信息,您可以阅读php.net/manual/en/function.func-get-args.php。您可以将它用于任何功能,并且可以正常工作。
    • 太棒了。一种丑陋的方法,但比替代方法更好。实际上,我对 PHP 中的很多东西都有这种感觉。
    • 我应该更具体地说 当 index() 中没有任何参数时
    • 我不知道这是否对您有帮助,但最好遵循一些已知的结构来最小化您的网址。本文将真正帮助您实现这一目标web-and-development.com/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多