简体   繁体   English

使用CodeIgniter将多个URL段转换为索引函数

[英]Mutliple URL Segments to Index Function With CodeIgniter

Please excuse me if this is an incredibly stupid question, as I'm new to CodeIgniter. 如果这是一个令人难以置信的愚蠢问题,请原谅我,因为我是CodeIgniter的新手。

I have a controller for my verification system called Verify. 我的验证系统有一个名为Verify的控制器。 I'd like to be able to use it something like site.com/verify/123/abcd , but I only want to use the index function, so both URL segments need to go to it. 我希望能够使用像site.com/verify/123/abcd这样的东西,但我只想使用索引功能,因此两个URL段都需要转到它。

I'm sure this can be done with URL routing somehow, but I can't figure out how to pass both URL segments into Verify's index function.. 我确信这可以通过URL路由以某种方式完成,但我无法弄清楚如何将两个URL段传递给Verify的索引函数。

Something like this in routes.php should do the job: routes.php中的这样的东西应该做的工作:

$route['verify/(:any)/(:any)'] = "verify/index/$1/$2"; $ route ['verify /(:any)/(:any)'] =“verify / index / $ 1 / $ 2”;

I'm pretty sure you can just pass any controller method in CodeIgniter multiple arguments without modifying routes or .htaccess unless I misunderstood the problem. 我很确定你可以在CodeIgniter多个参数中传递任何控制器方法,而无需修改路由或.htaccess,除非我误解了这个问题。

function index($arg_one, $arg_two)
{

}

$arg_one representing the 123 and $arg_two representing the abcd in your example URI. $ arg_one表示123和$ arg_two表示示例URI中的abcd。

您将需要编辑路由或编写htaccess规则,但是我不明白为什么要限制索引函数。

If you didnt wanna use routes for some reason, then you could add this function to the controller in question. 如果您因某些原因不想使用路线,那么您可以将此功能添加到相关控制器中。

public function _remap($method_in, $params = array()) {
    $method = 'process_'.$method_in;
    if (method_exists($this, $method)) {
        return call_user_func_array(array($this, $method), $params);
    }
    array_unshift($params, $method_in);
    $this->index($params);
}

Basically it does the same as default behavior in CI, except instead of sending a 404 on 'cant find method', it sends unfound method calls to the index. 基本上它与CI中的默认行为相同,除了不在“无法查找方法”上发送404,它会向索引发送未完成的方法调用。

You would need to alter your index function to take an array as the first argument. 您需要更改索引函数以将数组作为第一个参数。 OR if you know that you only ever want 2 arguments, you could change the last 2 lines to 或者,如果您知道自己只需要2个参数,则可以将最后2行更改为

$this->index($method_in, $params[0]);

Of course both solutions fail in someone uses an argument which is the same as a method in your controller. 当然,有些人使用与控制器中的方法相同的参数,两个解决方案都失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM