Php Codeigniter中的函数是_home()
据我所知,Wordpress有Php Codeigniter中的函数是_home(),php,codeigniter,codeigniter-url,Php,Codeigniter,Codeigniter Url,据我所知,Wordpress有is_home()函数来确定主页。 在YII中,我使用这样的解决方案 事实上,在CI中,我曾多次面对它的必要性。例如,在标记中添加一些css类 所有我发现的 有人能帮我或写自己的解决方案吗 提前感谢如果需要,您可以使用$this->router->fetch_class()获取当前控制器和$this->router->fetch_method()获取方法 与您链接的Yii示例类似,您可以执行以下操作 $is_home = $this->router->f
is_home()
函数来确定主页。在YII中,我使用这样的解决方案 事实上,在CI中,我曾多次面对它的必要性。例如,在标记
中添加一些css类
所有我发现的有人能帮我或写自己的解决方案吗
提前感谢如果需要,您可以使用
$this->router->fetch_class()
获取当前控制器和$this->router->fetch_method()
获取方法
与您链接的Yii示例类似,您可以执行以下操作
$is_home = $this->router->fetch_class() === 'name_of_home_controller' ? true : false;
或者也要匹配方法
$is_home = ($this->router->fetch_class() === 'name_of_home_controller' && $this->router->fetch_method() === 'name_of_home_method') ? true : false;
这样,即使页面url是(假设home controller+方法是默认的),等等,只要它调用该控制器,它就会将$is_home设置为true
编辑:如果不想明确说明主控制器并将其设置为默认控制器,也可以使用($this->router->fetch_class()===$this->router->default_controller)
CI v3的更新:$this->router->fetch_class()
和$this->router->fetch_method()
在CI v3中被弃用。使用$this->router->class
和$this->router->method
。我只想在这里添加我的答案,因为另一种方法不适用于我修改过的CI版本
这个片段是我用来检测我们是否在主页上的
if (!$this->uri->segment(1)) {
// We are on the homepage
}
如果没有自定义辅助对象,请创建一个;否则,在任何自定义帮助文件中,只需粘贴以下任何代码片段,您就可以从Codeigniter中的任何位置使用
is_home()
function is_home()
{
$CI =& get_instance();
return (!$CI->uri->segment(1))? TRUE: FALSE;
}
或
为什么不干脆
public function name_of_home_method()
{
$data['is_home'] = true;
}
为了获得更好的答案,您应该在主页上介绍两条路线: 我假设在routes.php中有这样的默认控制器:
$route['default_controller'] = 'home';
- 没有默认控制器名称的主页(如: yoursite.com)
- 带有控制器名称的主页(如: yoursite.com/home)
public function name_of_home_method()
{
$data['is_home'] = true;
}
$route['default_controller'] = 'home';
if(!$this->uri->segment(1) || $this->uri->segment(1)=='home'){
// YOU ARE ON THE HOME
}