Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //buat file My_Router.php di application/core/
- <?php
- class MY_Router extends CI_Router {
- private $dbh;
- public function custom_route() {
- include(APPPATH.'config/database.php');
- $hostname= $db[$active_group]['hostname'];
- $username= $db[$active_group]['username'];
- $password= $db[$active_group]['password'];
- $dbname = $db[$active_group]['database'];
- $routes = [];
- try {
- $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
- $dbh-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "SELECT * FROM routes";
- foreach($dbh->query($sql) as $row) {
- $routes[$row['route']] = $row['to'];
- }
- $dbh = null;
- } catch (PDOException $e) {
- echo $e->getMessage();
- }
- return $routes;
- }
- protected function _set_routing() {
- if (file_exists(APPPATH.'config/routes.php'))
- {
- include(APPPATH.'config/routes.php');
- }
- if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
- {
- include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
- }
- if (isset($route) && is_array($route))
- {
- isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
- isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
- unset($route['default_controller'], $route['translate_uri_dashes']);
- $this->routes = array_merge($route, $this->custom_route());
- }
- if ($this->enable_query_strings) {
- if ( ! isset($this->directory))
- {
- $route = isset($_GET['route']) ? trim($_GET['route'], " \t\n\r\0\x0B/") : '';
- if ($route !== '')
- {
- $part = explode('/', $route);
- if ( ! empty($part[1])) {
- $this->uri->filter_uri($part[0]);
- $this->set_directory($part[0]);
- $this->set_class($part[0]);
- if ( ! empty($part[1]))
- {
- $this->uri->filter_uri($part[1]);
- $this->set_method($part[1]);
- }
- $this->uri->rsegments = array(
- 1 => $this->class,
- 2 => $this->method
- );
- }
- } else {
- $this->_set_default_controller();
- }
- }
- return;
- }
- if ($this->uri->uri_string !== '')
- {
- $this->_parse_routes();
- }
- else
- {
- $this->_set_default_controller();
- }
- }
- }
- //contoh struktur dan data database
- CREATE TABLE `routes` (
- `id` int(11) NOT NULL,
- `route` varchar(200) NOT NULL,
- `to` varchar(200) NOT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- INSERT INTO `routes` (`id`, `route`, `to`) VALUES
- (1, 'tes', 'welcome/route_in_database');
- ALTER TABLE `routes`
- ADD PRIMARY KEY (`id`);
- //buat controller application/controller/Welcome.php
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class Welcome extends CI_Controller {
- public function route_in_database() {
- echo "Hello";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement