Welcome Guest, Not a member yet? Register   Sign In
fallback controller
#1

[eluser]furnissg[/eluser]
Hi, im new to CI and looking for a way to speed up my development. Ive been playing around with the framework and like what it does. I have come across an issue that im struggling with.

What i want to does is have a standard url structure, and use the controller if it exists but fallback to use a default controller if not.

for example i would have a general page controller as default which loads in the page content from the database,

so

mysite.com
mysite.com/about
mysite.com/contact

would all use my page controller and index function

but if i had a controller that existed it would use that

mysite.com/forum

would use the forum controller

essentially can you set it somewhere so if the controller does not exist it uses the default controller and index function?

Any help would be appreciated.

Thanks in advance.
#2

[eluser]InsiteFX[/eluser]
You set the default controller in application/config/routes.php

InsiteFX
#3

[eluser]furnissg[/eluser]
Yeah i have set that, the problem is if my url is mysite.com/about i want the "about" section if it is not a controller to be a parameter in the default controller index function. at the moment it goes to 404
#4

[eluser]InsiteFX[/eluser]
You can also read this:

CodeIgniter User Guide URI Rounting

InsiteFX
#5

[eluser]furnissg[/eluser]
Thanks, I tried using the routes config which half worked in that i could route everything through unless i had added a route. The problem with this method is that i would have to write a rule for each controller added, which isnt ideal if working on dynamic sites.

The solution i went forwhich seems to work well is from http://maestric.com/doc/php/codeigniter_404

All i chaged was the cntroller and method to

Code:
var $error_controller = 'home';
var $error_method_404 = 'index';

to run through the method i need.

All i have to do now is implement a cstom 404 when a page doesnt exist.
#6

[eluser]Line 210[/eluser]
I think I understand what you are trying to achieve.

You could try creating a base controller and extending all your controllers from that.

For example:

Code:
<?php
class MY_Controller extends Controller{

     function index()
     {
        //do stuff here...
        $this->load->view('name of default view');
     }
}

Save that in your application/libraries folder as MY_Controller.php.

Create a new controller in application/controllers like this:

Code:
<?php

class Forum extends MY_Controller{

      //You can leave this out if you want to inherit the default method from the parent  
      //class or if you need to do something custom in this class implement the same
      //method here.
     function index()
     {
        $this->load->view('name of forum view');
     }

}

Instead of extending the Base CI Controller you extend your new base controller named MY_Controller. Since you are extending your base class the new child class will inherit all of the methods of the parent. When needed you can override any of the base methods by implementing custom ones in the child class.

Read more about this here:
http://ellislab.com/codeigniter/user-gui...asses.html

Scroll down to extending core classes.

Let me know if this is what you were looking to do.




Theme © iAndrew 2016 - Forum software by © MyBB