[英]any way to cache routes.php file in codeigniter?
我正在嘗試從CI中的數據庫添加動態路由。 但是在向表中添加數據后,我的網站真的很慢。
無論如何執行routes.php文件? 還是以加快我的網站速度?
注意:我有超過60,000條SEO友好URL記錄。
路由影響了我的響應服務器時間,大約是4秒。
這是我的代碼:
require_once(BASEPATH . 'database/DB' . EXT);
require_once(BASEPATH . 'helpers/url_helper' . EXT);
require_once(BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();
$query = $db->query('select id,title from news');
$result = $query->result();
foreach ($result as $row) {
$string = strtolower(str_replace(' ', '-', $row->title));
$route["funny-news/" . $string] = "news/newsDetails/$row->id";
}
謝謝。
編輯:
newsDetails控制器代碼:
public function newsDetails($id)
{
$hdata['active'] = "news";
$result['news'] = $this->mytestdb->getNewsById($id);
$this->load->view('nheader', $hdata);
$this->load->view('newsDetails', $result);
$this->load->view('footer');
}
我的建議是建立自己的子類(MY_Controller)。在您的控制器中加載數據庫,並將路由直接寫入具有file_put_contents的路由文件中。
打開您的routes.php
,並將以下行放入
$route['funny-news/(.*)'] = "news/newsDetails/$1";
您的控制器應該看起來像
public function newsDetails($slug)
{
$hdata['active'] = "news";
$result['news'] = $this->mytestdb->getNewsBySlug($slug);
$this->load->view('nheader', $hdata);
$this->load->view('newsDetails', $result);
$this->load->view('footer');
}
一件事-您應該在數據庫中使用一個額外的字段來存儲所需的數據塊。
一個函數可以在這里找到PHP函數來生成slug(URL字符串) 。
使用這種方法,您不需要用不必要的路由填充數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.