Advertisement
Guest User

ROute in Database

a guest
Nov 26th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1.  
  2. //buat file My_Router.php di application/core/
  3.  
  4. <?php
  5.  
  6. class MY_Router extends CI_Router {
  7.    
  8.     private $dbh;
  9.  
  10.     public function custom_route() {
  11.         include(APPPATH.'config/database.php');
  12.  
  13.         $hostname= $db[$active_group]['hostname'];
  14.         $username= $db[$active_group]['username'];
  15.         $password= $db[$active_group]['password'];
  16.         $dbname = $db[$active_group]['database'];
  17.  
  18.         $routes = [];
  19.  
  20.         try {
  21.             $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
  22.  
  23.             $dbh-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  24.  
  25.             $sql = "SELECT * FROM routes";
  26.             foreach($dbh->query($sql) as $row) {
  27.                 $routes[$row['route']] = $row['to'];
  28.             }
  29.  
  30.             $dbh = null;
  31.         } catch (PDOException $e) {
  32.             echo $e->getMessage();
  33.         }
  34.  
  35.         return $routes;
  36.  
  37.     }
  38.  
  39.     protected function _set_routing() {
  40.  
  41.         if (file_exists(APPPATH.'config/routes.php'))
  42.         {
  43.             include(APPPATH.'config/routes.php');
  44.         }
  45.  
  46.         if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
  47.         {
  48.             include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
  49.         }
  50.  
  51.         if (isset($route) && is_array($route))
  52.         {
  53.             isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
  54.             isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
  55.             unset($route['default_controller'], $route['translate_uri_dashes']);
  56.             $this->routes  = array_merge($route, $this->custom_route());
  57.         }
  58.  
  59.  
  60.         if ($this->enable_query_strings) {
  61.  
  62.             if ( ! isset($this->directory))
  63.             {
  64.                 $route = isset($_GET['route']) ? trim($_GET['route'], " \t\n\r\0\x0B/") : '';
  65.  
  66.                 if ($route !== '')
  67.                 {
  68.                     $part = explode('/', $route);
  69.  
  70.                     if ( ! empty($part[1])) {
  71.  
  72.                         $this->uri->filter_uri($part[0]);
  73.                         $this->set_directory($part[0]);
  74.                         $this->set_class($part[0]);
  75.  
  76.                         if ( ! empty($part[1]))
  77.                         {
  78.                             $this->uri->filter_uri($part[1]);
  79.                             $this->set_method($part[1]);
  80.                         }
  81.  
  82.                         $this->uri->rsegments = array(
  83.                             1 => $this->class,
  84.                             2 => $this->method
  85.                         );
  86.                     }
  87.  
  88.                 } else {
  89.  
  90.                     $this->_set_default_controller();
  91.                 }
  92.             }
  93.  
  94.             return;
  95.         }
  96.  
  97.         if ($this->uri->uri_string !== '')
  98.         {
  99.             $this->_parse_routes();
  100.         }
  101.         else
  102.         {
  103.             $this->_set_default_controller();
  104.         }
  105.     }
  106. }
  107.  
  108.  
  109. //contoh struktur dan data database
  110.  
  111. CREATE TABLE `routes` (
  112.   `id` int(11) NOT NULL,
  113.   `route` varchar(200) NOT NULL,
  114.   `to` varchar(200) NOT NULL
  115. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  116.  
  117.  
  118. INSERT INTO `routes` (`id`, `route`, `to`) VALUES
  119. (1, 'tes', 'welcome/route_in_database');
  120.  
  121. ALTER TABLE `routes`
  122.   ADD PRIMARY KEY (`id`);
  123.  
  124.  
  125. //buat controller application/controller/Welcome.php
  126.  
  127. <?php
  128. defined('BASEPATH') OR exit('No direct script access allowed');
  129.  
  130. class Welcome extends CI_Controller {
  131.  
  132.     public function route_in_database() {
  133.         echo "Hello";
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement