简体   繁体   English

代码点火器控制器uri段

[英]code igniter controller uri segment

i'm trying to get the segment depth that triggered the controller inside my controller. 我试图获取触发控制器内部控制器的分段深度。

for example: i have controllers/data/something.php 例如:我有controllers / data / something.php

class Something extends CI_Controller {
    public function index()
    {
        $depth = ???;   // should be 2
    }
}

would load through www.domain.com/data/something 将通过www.domain.com/data/something加载

counting the segments would be irrelevant since i could go to: www.domain.com/data/something/anotherthing/ 计算段数将是无关紧要的,因为我可以去:www.domain.com/data/something/anotherthing/

and still run the previous controller, but depth should remain 2 并仍运行先前的控制器,但深度应保持2

any idea how it can be done? 任何想法如何做到这一点?

EDIT to clarify: 编辑以澄清:

i want the depth of the part in the uri that triggered the controller (ignoring custom routing). 我想要触发控制器的uri中零件的深度(忽略自定义路由)。

i don't want all the uri elements, since there could be more elements after the controller/method. 我不希望所有uri元素,因为在控制器/方法之后可能会有更多元素。

eg. 例如。 domain.com/controller_name/method_name/param1/param2 domain.com/controller_name/method_name/param1/param2

only the first 2 parts of the segments triggered the controller 段的前2个部分触发了控制器

what i have so far is: 到目前为止,我有:

public function route_segments()
{
    $path = dirname(BASEPATH) . '/'. APPPATH;
    $request = substr(str_replace('\\','/',__FILE__), strlen($path) - strlen(EXT));
    $request = substr($request, strpos($request, '/'));
    $trigger_path = '';
    for ($i=1, $n = $this->uri->total_segments(); $i<=$n && $trigger_path != $request; $i++){
        $trigger_path .= '/'.$this->uri->segment($i);
    }
    $route_segments = explode('/',trim($trigger_path,'/'));
    return count($route_segments);
}

problem is i don't want to use __FILE__ since i want it in a parent controller 问题是我不想使用__FILE__,因为我希望在父控制器中使用它

SOLUTION: 解:

added as an answer 添加为答案

i removed it from the post and added it as an answer: 我从帖子中删除了它,并将其添加为答案:

after seeing @Jorge's comment suggesting to use CI routes, i found a simple solution: 在看到@Jorge的建议使用CI路由的评论后,我找到了一个简单的解决方案:

private function get_route_depth()
{
    $this->load->library('router');
    $route_segments = explode('/',trim($this->router->directory,'/'));
    return count($route_segments);
}

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

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