CodeIgniter: How to get Controller, Action, URL in

2019-01-16 02:48发布

I have these URLs:

How to get controller name, action name from these URLs. I'm CodeIgniter newbie. Are there any helper function to get this info

Ex:

$params = helper_function( current_url() )

Where $params becomes something like

array (
  'controller' => 'system/settings', 
  'action' => 'edit', 
  '...'=>'...'
)

10条回答
相关推荐>>
2楼-- · 2019-01-16 03:10

If you using $this->uri->segment , if urls rewriting rules change, segments name matching will be lost.

查看更多
【Aperson】
3楼-- · 2019-01-16 03:10
$this->router->fetch_class(); 

// fecth class the class in controller $this->router->fetch_method();

// method

查看更多
Root(大扎)
4楼-- · 2019-01-16 03:11

Instead of using URI segments you should do this:

$this->router->fetch_class(); // class = controller
$this->router->fetch_method();

That way you know you are always using the correct values even if you are behind a routed URL, in a sub-domain, etc.

查看更多
不美不萌又怎样
5楼-- · 2019-01-16 03:11

Update

The answer was added was in 2015 and the following methods are deprecated now

$this->router->fetch_class();  in favour of  $this->router->class; 
$this->router->fetch_method(); in favour of  $this->router->method;

Hi you should use the following approach

$this->router->fetch_class(); // class = controller
$this->router->fetch_method(); // action

for this purpose but for using this you need to extend your hook from the CI_Controller and it works like a charm, you should not use uri segments

查看更多
来,给爷笑一个
6楼-- · 2019-01-16 03:13

As an addition

$this -> router -> fetch_module(); //Module Name if you are using HMVC Component
查看更多
你好瞎i
7楼-- · 2019-01-16 03:16

You could use the URI Class:

$this->uri->segment(n); // n=1 for controller, n=2 for method, etc

I've also been told that the following work, but am currently unable to test:

$this->router->fetch_class();
$this->router->fetch_method();
查看更多
登录 后发表回答