php - CodeIgniter - Generate routes dynamically -


i have website dynamic navigational menu. keep controller (portuguese) names in database, translation english.

i want know if possible affect 'route' array @ runtime, create routes , cache when page loaded.

i hope clear enough, help

you this:

create table named routes

-- -- table structure 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 ; 

create file in config directory named pdo_db_connect.php

put inside , change accordingly.

<?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(); }  } 

now in routes file can this:

<?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 * routes order `order` asc');              if($routes_query){                 $return_data = array();                  foreach($routes_query $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 $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();     // route data     $route_data = $dynamic_route->get_routes();     //iterate on routes     foreach($route_data $row){         $route[$row->url] = $row->route;     } 

Comments