php CodeIgniter:如何获取Controller、Action、URL信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2062086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:51:54  来源:igfitidea点击:

CodeIgniter: How to get Controller, Action, URL information

phpcodeignitercodeigniter-2

提问by noname.cs

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

如何从这些 URL 中获取控制器名称、操作名称。我是 CodeIgniter 新手。是否有任何帮助函数来获取此信息

Ex:

前任:

$params = helper_function( current_url() )

Where $paramsbecomes something like

哪里$params变得像

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

回答by Sampson

You could use the URI Class:

您可以使用URI 类

$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();

回答by Phil Sturgeon

Instead of using URI segments you should do this:

您应该这样做,而不是使用 URI 段:

$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.

这样你就知道你总是使用正确的值,即使你在路由 URL 后面,在子域中,等等。

回答by lumos0815

The methods are deprecated.

这些方法已被弃用。

$this->router->fetch_class();
$this->router->fetch_method();

You can access the properties instead.

您可以改为访问属性。

$this->router->class;
$this->router->method;

See codeigniter user guide

请参阅codeigniter 用户指南

URI Routing methods fetch_directory(), fetch_class(), fetch_method()

With properties CI_Router::$directory, CI_Router::$classand CI_Router::$methodbeing public and their respective fetch_*()no longer doing anything else to just return the properties - it doesn't make sense to keep them.

Those are all internal, undocumented methods, but we've opted to deprecate them for now in order to maintain backwards-compatibility just in case. If some of you have utilized them, then you can now just access the properties instead:

$this->router->directory;
$this->router->class;
$this->router->method;

URI 路由方法 fetch_directory()、fetch_class()、fetch_method()

使用 properties CI_Router::$directoryCI_Router::$class并且 CI_Router::$method是 public 并且它们各自fetch_*()不再做任何其他事情来返回属性 - 保留它们没有意义。

这些都是内部的、未记录的方法,但我们现在选择弃用它们,以保持向后兼容性以防万一。如果你们中的一些人已经使用了它们,那么您现在可以改为访问属性:

$this->router->directory;
$this->router->class;
$this->router->method;

回答by Luis Chanferoni

Another way

其它的办法

$this->router->class

回答by Starx

As an addition

作为补充

$this -> router -> fetch_module(); //Module Name if you are using HMVC Component

回答by Muhammad Omer Aslam

Update

更新

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

答案是在 2015 年添加的,现在不推荐使用以下方法

$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_Controllerand it works like a charm, you should not use uri segments

为此目的,但要使用它,您需要从 扩展您的钩子CI_Controller,它就像一个魅力,您不应该使用 uri 段

回答by Aslam Patel

Use This Code anywhere in class or libraries

在类或库中的任何地方使用此代码

    $current_url =& get_instance(); //  get a reference to CodeIgniter
    $current_url->router->fetch_class(); // for Class name or controller
    $current_url->router->fetch_method(); // for method name

回答by monsterm

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

如果你使用 $this->uri->segment ,如果 url 重写规则改变,段名称匹配将丢失。

回答by PrakashG

Last segment of URL will always be the action. Please get like this:

URL 的最后一段将始终是操作。请像这样:

$this->uri->segment('last_segment');

回答by Fel

$this->router->fetch_class(); 

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

// 将控制器中的类命名为 $this->router->fetch_method();

// method

// 方法