i'm trying to get the segment depth that triggered the controller inside my controller.
for example: i have controllers/data/something.php
class Something extends CI_Controller {
public function index()
{
$depth = ???; // should be 2
}
}
would load through www.domain.com/data/something
counting the segments would be irrelevant since i could go to: www.domain.com/data/something/anotherthing/
and still run the previous controller, but depth should remain 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).
i don't want all the uri elements, since there could be more elements after the controller/method.
eg. domain.com/controller_name/method_name/param1/param2
only the first 2 parts of the segments triggered the controller
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
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:
private function get_route_depth()
{
$this->load->library('router');
$route_segments = explode('/',trim($this->router->directory,'/'));
return count($route_segments);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.