我有一個帶有動態導航菜單的網站。我將控制器(葡萄牙語)名稱與翻譯成英文的數據庫保存在一起。CodeIgniter - 動態生成路由
我想知道是否有可能在運行時影響'路由'數組,因此它會創建這些路線並在頁面加載時進行緩存。
我希望我很清楚,感謝您的幫助
我有一個帶有動態導航菜單的網站。我將控制器(葡萄牙語)名稱與翻譯成英文的數據庫保存在一起。CodeIgniter - 動態生成路由
我想知道是否有可能在運行時影響'路由'數組,因此它會創建這些路線並在頁面加載時進行緩存。
我希望我很清楚,感謝您的幫助
你可以這樣做:
創建一個表命名路由
--
-- Table structure for table `Routes`
--
CREATE TABLE IF NOT EXISTS `Routes` (
`idRoutes` int(11) NOT NULL AUTO_INCREMENT,
`Order` int(11) NOT NULL,
`Url` varchar(250) NOT NULL,
`Url_Variable` varchar(20) NOT NULL,
`Class` text NOT NULL,
`Method` text NOT NULL,
`Variable` text NOT NULL,
`Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`idRoutes`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=67 ;
中創建一個名爲pdo_db_connect.php
將這個裏面的配置目錄中的文件,並相應地改變。
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
function pdo_connect(){
try{
// Include database info
include 'database.php';
if(!isset($db)){
echo 'Database connection not available!';
exit;
}
$dbdriver = $db['default']['dbdriver'];//'mysql';
$hostname = $db['default']['hostname'];//'localhost';
$database = $db['default']['database'];//'config';
$username = $db['default']['username'];//'root';
$password = $db['default']['password'];//'password';
//to connect
$DB = new PDO($dbdriver.':host='.$hostname.'; dbname='.$database, $username, $password);
return $DB;
}catch(PDOException $e) {
echo 'Please contact Admin: '.$e->getMessage();
}
}
在你的路由
現在文件,你可以這樣做:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
// Include our PDO Connection
include('application/config/pdo_db_connect.php');
class dynamic_route{
public $pdo_db = FALSE;
public function __construct(){
}
private function query_routes(){
try{
$routes_query = $this->pdo_db->query('SELECT * FROM Routes ORDER BY `Order` ASC');
if($routes_query){
$return_data = array();
foreach($routes_query as $row) {
$return_data[] = $row;
}
return $return_data;
}
}catch(PDOException $e) {
echo 'Please contact Admin: '.$e->getMessage();
}
}
private function filter_route_data($data){
$r_data = array();
foreach($data as $row){
$return_data = new stdClass;
if(empty($row['Url_Variable'])){
$return_data->url = $row['Url'];
}else{
$return_data->url = $row['Url'].'/'.$row['Url_Variable'];
}
if(empty($row['Method']) && empty($row['Variable'])){
$return_data->route = $row['Class'];
}elseif(!empty($row['Method']) && empty($row['Variable'])){
$return_data->route = $row['Class'].'/'.$row['Method'];
}elseif(!empty($row['Method']) && !empty($row['Variable'])){
$return_data->route = $row['Class'].'/'.$row['Method'].'/'.$row['Variable'];
}
$r_data[] = $return_data;
}
return $r_data;
}
public function get_routes(){
$route_data = $this->query_routes();
$return_data = $this->filter_route_data($route_data);
return $return_data;
}
}
$dynamic_route = new dynamic_route;
// Give dynamic route database connection
$dynamic_route->pdo_db = pdo_connect();
// Get the route data
$route_data = $dynamic_route->get_routes();
//Iterate over the routes
foreach($route_data as $row){
$route[$row->url] = $row->route;
}
記住路線文件只是一個包含數組的PHP文件,所以如果你想讓你的「黑客」T恤上你可以輕鬆地做一些有點骯髒。
讓你的CMS /應用程序/網絡應用/冰箱監控系統/無論有一個接口,創建和存儲數據庫中的記錄。然後,無論何時保存,將此內容放入application/cache/routes.php。
最後,你只需要你的主要routes.php包括緩存版本,你很好去。
您需要以下信息。像這樣將您的路線保存到應用程序/緩存文件夾名爲「routes.php文件」文件
一個控制器:
public function save_routes()
{
// this simply returns all the pages from my database
$routes = $this->Pages_model->get_all($this->siteid);
// write out the PHP array to the file with help from the file helper
if (!empty($routes)) {
// for every page in the database, get the route using the recursive function - _get_route()
foreach ($routes->result_array() as $route) {
$data[] = '$route["' . $this->_get_route($route['pageid']) . '"] = "' . "pages/index/{$route['pageid']}" . '";';
}
$output = "<?php if (! defined('BASEPATH')) exit('No direct script access allowed');\n";
$output .= implode("\n", $data);
$this->load->helper('file');
write_file(APPPATH . "cache/routes.php", $output);
}
}
這裏有你需要的第二功能。這一次和一個前都將走在你的應用程序控制器,處理您的網頁:
// Credit to http://acairns.co.uk for this simple function he shared with me
// this will return our route based on the 'url' field of the database
// it will also check for parent pages for hierarchical urls
private function _get_route($id)
{
// get the page from the db using it's id
$page = $this->Pages_model->get_page($id);
// if this page has a parent, prefix it with the URL of the parent -- RECURSIVE
if ($page["parentid"] != 0)
$prefix = $this->_get_route($page["parentid"]) . "/" . $page['page_name'];
else
$prefix = $page['page_name'];
return $prefix;
}
在你Pages_model你需要的東西沿着這條線:
function get_page($pageid) {
$this->db->select('*')
->from('pages')
->where('pageid', $pageid);
$query = $this->db->get();
$row = $query->row_array();
$num = $query->num_rows();
if ($num < 1)
{
return NULL;
} else {
return $row;
}
}
而且這樣的:
function get_all($siteid) {
$this->db->select('*')
->from('pages')
->where('siteid', $siteid)
->order_by('rank');
;
$query = $this->db->get();
$row = $query->row_array();
$num = $query->num_rows();
if ($num < 1)
{
return NULL;
} else {
return $query;
}
}
每次創建頁面時,都需要執行此行,因此我建議在完成所有骯髒工作後將其放在控制器的末尾:
$this->save_routes();
所有這一切將使您成爲一個甜蜜的路線文件,將不斷更新,只要事情發生變化。
東西並不複雜,它會持續友好的URL:
內routes.php文件中:
// Routes from the database
require_once (BASEPATH .'database/DB.php');
$db =& DB();
// slug will be something like
// 'this-is-a-post'
// 'this-is-another-post'
$sql = "SELECT id, slug FROM items";
$query = $db->query($sql);
$result = $query->result();
foreach($result as $row)
{
// first is if multiple pages
$route["$row->slug/(:num)"] = "item/index/$row->id";
$route["$row->slug"] = "item/index/$row->id";
}
這個假設'id'作爲主鍵,'slug'將是唯一的
內部控制器,可以通過獲得ID:
$id = $this->uri->rsegment(3);
凱爾,我沒有辦法來測試這是我甚至用PHP的工作不再很長一段時間了。你測試過了嗎?如果你告訴我你確定這段代碼有效,我會接受你的答案,因爲它似乎正是我要求的 – 2014-01-06 08:35:26
是的,它有效,我有一個應用程序,即時通訊工作,使用它。 – jphreak 2014-01-06 23:57:56