Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter中的函数是_home()_Php_Codeigniter_Codeigniter Url - Fatal编程技术网

Php Codeigniter中的函数是_home()

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

据我所知,Wordpress有
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)


好吧,我建议你自己定义“家”的含义。它是“/”的请求URI还是其他URI。如果是这样的话,检查请求URI以查看是否匹配应该非常简单。无意冒犯,但是Laravel使这件愚蠢的事情变得如此简单。。。。它可能是php框架中最好的路由器机制
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
}