Vuthy Sok Vuthy Sok - 2 months ago 22
PHP Question

Codeigniter Route url parents page and child page

Edit: I am new Codeigniter I am not how to use Codeigniter Routing. I create Contact Us page and Map page. Map page is the subpage of Contact Us page.

Table Name : Pages

id label link parent
1 Contact Us contact-us 0
3 About Us about-us 0
2 Map map 1


Here my Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('page_model');
}
public function index()
{

$data['getAllPage'] = $this->page_model->getAllPages();

$this->load->view('page_listing',$data);

}
public function view($id) {
$data['single_page'] = $this->page_model->displaySinglePage($id);
$this->load->view('single_page',$data);
}
}


In routes.php I have put $route['(:any)'] = "page/view/$1";

When I enter url "http://mytest.dev/contact-us/" or "http://mytest.dev/about-us/" it show correct content of Contact page but I enter "http://mytest.dev/contact-us/map" it still show content of Contact page.

What I want when I enter "http://mytest.dev/contact-us/map" it shuold show content of Map page

Thanks in advance.

Answer

Please try code maybe can help

// Parents and Child page.
$route['page/(:any)/(:num)'] = "page/view/$1/$2";

// For Main Home Page.
$route['(:any)'] = "page/view/$1";