CodeIgniter Forums
Controller not found -> Use this controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Controller not found -> Use this controller (/showthread.php?tid=9876)



Controller not found -> Use this controller - El Forum - 07-11-2008

[eluser]Chicken's Egg[/eluser]
CodeIgniter can load a controller by default. After a plain installation that is the welcome.php controller. I searched the user guide, but I didn't find an answer on the following question. Is it possible to get something like.
- Of no controller is given in the uri -> use default controller. (for example welcome.php)
- If a controller is specified -> use specified controller. (for example: weblog, shop, forum)
- If that specified controller doesn't exist / is not found -> use this other by CI specified controller. (for example text.php)

So:
www.mysite.com | Uses welcome.php
www.mysite.com/blog/ | Use blog.php
www.mysite.com/mypage/ | Use text.php

Goal: I would like to have a possibility to add some text pages to my website aswell, usinng CI, but without telling CI that they can be viewed using the controller text.

I have been thinking about a solution too. I could create a route using regular expression and check if the asked controller exists. But: I don't know if all regular expression functions can be used and it is hard coded. I would rather prefer a more flexible solution.


Controller not found -> Use this controller - El Forum - 07-11-2008

[eluser]xwero[/eluser]
This tutorial should get you on your way.


Controller not found -> Use this controller - El Forum - 07-11-2008

[eluser]Chicken's Egg[/eluser]
But that tutorial is about getting a default view for the controller method. Or did I miss the point?


Controller not found -> Use this controller - El Forum - 07-11-2008

[eluser]xwero[/eluser]
That is one example of customization. For the thing you want you need to change
Code:
if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
{
    show_error('Unable to load your default controller.  Please make sure the controller specified in your Routes.php file is valid.');
}

include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
located at line 149, to
Code:
$class = $RTR->fetch_class();

if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$class.EXT))
{
    // default controller
        $class = 'text';
}

include(APPPATH.'controllers/'.$RTR->fetch_directory().$class.EXT);
But then you also need to remove the line (at 169)
Code:
$class  = $RTR->fetch_class();

The rest of your requested behavior is already in CI so that doesn't need to be changed.


Controller not found -> Use this controller - El Forum - 07-11-2008

[eluser]Chicken's Egg[/eluser]
So may be even this would work:

Code:
if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
{
  if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().text.EXT))
  {
    show_error('Unable to load your default controller.  Please make sure the controller specified in your Routes.php file is valid.');
  }
}

Well, I'm gonna play a little bit more with it. This sounds a great opportunity. Thank you very much for your help.


Controller not found -> Use this controller - El Forum - 07-11-2008

[eluser]xwero[/eluser]
The class variable is used further in the script so somewhere you need to give it a value.

I think you want to add the string text but you forgot to put it inside quotes Wink