php - How to overwrite Codeigniter 3 route to show just name instead id and name -



need little guidance here. i'm using codeigniter 3. since mvc fw segment routing, want know how possible create custom route show name of record (it can category, product, post etc..) returned database instead of id/name when need id segment identify record database need return or preview id.

i post code example:

controller

class categories extends ci_controller {     function __construct()     {         parent::__construct();         $this->load->model('categorym');     }      public function index(){         $data = array(             'category_list' => $this->categorym->listall()             );          $this->load->view('test/category_list', $data);     }      public function preview()     {         $categoryid = $this->uri->segment(2);          $data = array (             'previewbycategory' => $this->categorym->previewbycategory($categoryid)             );          $this->load->view('public/preview_by_category', $data);     } 

model

<?php      class categorym extends ci_model {          public function listall() {              $query = $this->db             ->select('*')              ->from('categories')              ->where('parentid', null, true)             ->order_by('name', 'asc')              ->get();              if($query->num_rows() > 0) {                 return $query->result();             }else{                 return false;             }              public function previewbycategory($categoryid=''){                  $query = $this->db                 ->select( /* posts , categories data */ )                 ->from('categories')                 ->join('posts', 'posts.categoryid = categories.id', 'left')                 ->where('categories.id', $categoryid)                 ->get();                 if( $query->num_rows() > 0 ){                     return $query->result();                 }else{                     return false;                 }             }          } 

view

<?php if($category_list):?>     <?php foreach($category_list $category):?>         <h3>              <a href="<?php echo base_url() . strtolower(url_title($category->name) . '/' . $category->id);?>">                 <?php echo $category->name;?>             </a>         </h3>     <?php endforeach;?> <?php endif;?> 

my routes

// category routes $route['categories'] = 'categories/index'; $route['(:any)/(:num)'] = 'categories/preview/$1'; 

what want instead of routes

www.mywebsite/categoryname/categoryid/ 

to display

www.mywebsite/categoryname 

which list products category group. how without id in url. i'm sorry if question broad. thank's in advance.

codeigniter not allow database access in routes.
can create database instance manually.

routes.php

$slug= ($this->uri->segment(1)) ? $this->uri->segment(1) : false; if($slug){   //include database    require_once(basepath."/database/db.php");   $db=& db();   $category = $this->db               ->select('name')                ->from('categories')                ->where('parentid', null, true)               ->where('name',$slug)               ->get()->row();   if($category){     //$category->name must unique in database     $route[$category->name] = 'categories/index';   }  } 

in categories controller

class categories extends ci_controller {     function __construct()     {         parent::__construct();         $this->load->model('categorym');     }      public function index(){       echo $category_name=$this->uri->segment(1);     } } 

url

www.mywebsite/categoryname

//if categoryname exist in category 

output :

categoryname


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -