php - How do you use multiple controllers in CodeIgniter? -


i'm new codeigniter , i've gone through of user guide me understand how multiple controllers.

i have figured out how load multiple pages using 1 of examples. default controller, called site.php:

class site extends ci_controller {  public function index($page = 'home') {     if ( ! file_exists(apppath.'/views/'.$page.'.php')) {         // whoops, don't have page that!         show_404();     }      $data['title'] = ucfirst($page);      $this->load->view('header', $data);     $this->load->view($page, $data);     $this->load->view('footer', $data);      } } 

this works , routes.php:

$route['default_controller'] = 'site'; $route['(:any)'] = 'site/index/$1'; 

i have few views load correctly when go localhost/website/index.php/about, example.


now, further learn controllers , uri routing. created new controller, application/controllers/mycontroller.php , coded such:

class mycontroller extends ci_controller {     public function index() {         $this->load->view('header');         $this->load->view('my_controller');         $this->load->view('footer');     } } 

the thing is, don't know how access page. understand default uri pattern example.com/class/function/id/ have tried localhost/website/mycontroller , doesn't work. apache returns 404. i've tried many manipulations of this, going stupid typing localhost/website/index.php/mycontroller of course doesn't work.


what correct way use multiple controllers? have done wrong here? please assist me in learning, thank you!

(also, first proper question on stackoverflow, if i've done wrong or formatted incorrectly, please let me know!)

the problem rule:

$route['(:any)'] = 'site/index/$1'; 

intercepts everything... , passes site controller, argument index method. if call:

/mycontroller 

it map to:

/site/index/mycontroller 

the routing happens before app looks @ controllers, , routing rules considered in order written.

thus need put rule @ bottom of rule list in order let other rules work. adding in front:

$route['mycontroller'] = 'mycontroller'; $route['(:any)'] = 'site/index/$1'; 

it should work fine (although sligthly unusual approach, global any rule) first check if url requested /mycontroller , if so, call mycontroller; otherwise behave used site controller.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -