简体   繁体   中英

routing controller in codeigniter

My routes.php as Below:

$controller_list = array('showmenu','aboutus'); // etc you will have        to put all your controllers in this array

foreach($controller_list as $controller_name)
{
     $route[$controller_name] = $controller_name;
     $route[$controller_name.'/(:any)'] = $controller_name.'/$1';
}
$route['([a-zA-z_]+)'] = 'main/index/$1';
//$route['default_controller'] = "home";
$route['404_override'] = '';

I want to call all controller in main class like localhost/foldername/main/about(different slugs) how to do that?

i exactly don't understand what you want to say but you can define each function separately in your routes.php for simple and fine url like this:

$route['whatever you want in your url'] = 'your controller name/your function name';

this will like:

localhost/foldername/(different slugs)

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

    class Main extends CI_Controller {


public function __construct() {
    parent::__construct();
    $this->load->library('form_validation');
     $this->load->database();
    $this->load->model("menu_model");
     $this->load->model("main_model");
}

public function index() {


     $data['category']= $this->menu_model->getCategory('$lang');
    $data['subcategory']= $this->menu_model->getSubCategory('$lang');


    $this->load->view('vwHeader',$data);//Left Menu


}

 public function showmenu()
{
     $menutitle = $this->uri->segment(3);
      $query = $this->db->get_where('category',array('namecategory'=>$menutitle));
      $data['content'] = $this->main_model->show_content($menutitle);

    $data['category']= $this->menu_model->getCategory('$lang');
    $data['subcategory']= $this->menu_model->getSubCategory('$lang');

    $this->load->view('vwMain',$data);//Left Menu
}
 public function menu()
{
    $data['category']= $this->menu_model->getCategory('$lang');
    $data['subcategory']= $this->menu_model->getSubCategory('$lang');

    $this->load->view('vwHeader',$data);//Left Menu
}



       public function aboutus() {

       $arr['page'] ='about';
         $data['category']= $this->menu_model->getCategory('$lang');
    $data['subcategory']= $this->menu_model->getSubCategory('$lang');


    $this->load->view('vwHeader',$data);//Left Menu
    $this->load->view('vwAboutus',$arr);
}

         public function contactus() {

       $arr['page'] ='contactus';
         $data['category']= $this->menu_model->getCategory('$lang');
    $data['subcategory']= $this->menu_model->getSubCategory('$lang');


    $this->load->view('vwHeader',$data);//Left Menu
    $this->load->view('vwContactus',$arr);
}


 }

This is controller main.php

Try this first you have to set your default controller

$default_controller = "main";
$route['default_controller'] = $default_controller;

then you have to define your methods

$route['same url'] = 'aboutus';
$route['same url'] = 'showmenu';

in your routes.php

$default_controller = "main";
$route['default_controller'] = $default_controller;

$route['aboutus'] = 'aboutus';
$route['slug'] = 'showmenu';

$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';

$route['404_override'] = '';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM