因爲我知道Wordpress有is_home()
函數來確定主頁。
在誼我使用這樣Yii check if homepageCodeigniter中的is_home()函數
解決方案在CI,模板其實,我有它的必要性面臨着許多倍。例如,在標籤<body>
中添加一些css類。
所有我發現http://ellislab.com/forums/viewthread/194637/#916899
任何人都可以幫助我或自己寫解決方案嗎?
預先感謝
因爲我知道Wordpress有is_home()
函數來確定主頁。
在誼我使用這樣Yii check if homepageCodeigniter中的is_home()函數
解決方案在CI,模板其實,我有它的必要性面臨着許多倍。例如,在標籤<body>
中添加一些css類。
所有我發現http://ellislab.com/forums/viewthread/194637/#916899
任何人都可以幫助我或自己寫解決方案嗎?
預先感謝
您可以使用$this->router->fetch_class()
來獲得當前控制器和$this->router->fetch_method()
如果需要得到法了。
如此的相似,你鏈接到Yii的例子,你可以不喜歡
$is_home = $this->router->fetch_class() === 'name_of_home_controller' ? true : false;
或者,即使該網頁的網址爲http://yoursite.com/匹配方法太
$is_home = ($this->router->fetch_class() === 'name_of_home_controller' && $this->router->fetch_method() === 'name_of_home_method') ? true : false;
這樣(假設家庭控制器+方法是默認的),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
。
我只是想在這裏添加我的答案,因爲其他方法不適用於我的重大修改版本的CI。
這個片段是我用它來檢測,如果我們在主頁上
if (!$this->uri->segment(1)) {
// We are on the homepage
}
if($this->uri->uri_string() == ''){
echo"You are on homepage";
}
如果沒有自定義幫助創建一個;否則在您的任何自定義幫助程序文件中只需粘貼以下代碼片段,並且您應該可以在Codeigniter中的任何位置使用is_home()
。
function is_home()
{
$CI =& get_instance();
return (!$CI->uri->segment(1))? TRUE: FALSE;
}
OR
function is_home()
{
$CI =& get_instance();
return (strtolower($CI->router->fetch_class()) === $CI->router->default_controller && $CI->router->fetch_method() === 'index') ? true : false;
}
爲什麼不只是單純的
public function name_of_home_method()
{
$data['is_home'] = true;
}
嗯,我會建議確定自己是什麼 「家」 的意思。它是一個'/'或其他URI的請求URI。如果是這樣,檢查請求URI以查看它是否匹配應該很簡單。 – 2013-05-06 16:24:11
沒有冒犯,但Laravel使這個愚蠢的事情變得更容易....它可能有最好的路由器機制之間的PHP框架。 ;) – itachi 2013-05-06 17:08:13