And*_*ida 5 php codeigniter codeigniter-url
据我所知,Wordpress具有is_home()确定主页的功能.
在YII我使用这样的解决方案Yii检查是否主页
在CI中,模板实际上,我面对很多次需要它.例如,在标记中添加一些css类<body>.
所有我发现的http://ellislab.com/forums/viewthread/194637/#916899
任何人都可以帮助我或编写自己的解决方案吗?
预先感谢
Ada*_*dam 19
我只是想在这里添加我的答案,因为另一种方法不适用于我经过大量修改的CI版本.
这个片段是我用来检测我们是否在主页上的内容
if (!$this->uri->segment(1)) {
// We are on the homepage
}
Run Code Online (Sandbox Code Playgroud)
您可以使用$this->router->fetch_class()获取当前控制器并$this->router->fetch_method()在需要时获取该方法.
与您链接的Yii示例类似,您可以执行类似的操作
$is_home = $this->router->fetch_class() === 'name_of_home_controller' ? true : false;
Run Code Online (Sandbox Code Playgroud)
或者也是匹配方法
$is_home = ($this->router->fetch_class() === 'name_of_home_controller' && $this->router->fetch_method() === 'name_of_home_method') ? true : false;
Run Code Online (Sandbox Code Playgroud)
这种方式即使页面网址是http://yoursite.com/(假设主控制器+方法是默认设置),http://yoursite.com/home_controller/,http://yoursite.com/something/that/routes/to/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替代.
| 归档时间: |
|
| 查看次数: |
7733 次 |
| 最近记录: |