Welcome Guest, Not a member yet? Register   Sign In
Using functions in URI without controller
#1

[eluser]donaldpcook[/eluser]
Hi all,

Need routing help. I have an already established website that I want to re-do with CI, but I want to keep all of the current URLs. As of now they are like this:

www.website.com/subpage.html

I have a default controller that calls all the subpages, so if I put in this:

www.website.com/controller/subpage.html

it works.

Question is, how would I get rid of requiring the controller be entered in? I do have other controllers that I still want to be able to use, such as this:

www.website.com/logincontroller/subpage

Thanks in advance!
#2

[eluser]fchristant[/eluser]
You can set a default controller like this:

$route['default_controller'] = 'home';

This will make sure all URLS that do not match with any of your controllers will go via that controller. Within that controller, you load whatever page you want.

If you are using custom routes in the config/routes.php file, there is another trick you can use. As the last rule of your rule set, do something like this:

$route[':any'] = "error/f404";

This example makes sure that if no rule is matched it will route to the 404 page. However, you can of course also route it to your root controller.

I hope this helps.
#3

[eluser]donaldpcook[/eluser]
Using the default controller works for my index page, so instead of using www.website.com/controller/index I can just put in www.website.com. What about my other pages though? So that I can just do www.website.com/otherpage, which is a function in the default controller. If I do just www.website.com/function a 404 comes up.
#4

[eluser]fchristant[/eluser]
What if you use the following rules:

$route[’(:any)/(:any)/(:any)] = 'yourcontroller/$1/$2'
$route[’(:any)/(:any)] = 'yourcontroller/$1'
$route[’(:any)] = 'yourcontroller'

This makes sure any URL of deepness 1, 2 or 3 will route allways to your desired controller. Within your controller you can check for the params passed, if it concerns a page not existing, just do show_404();
#5

[eluser]donaldpcook[/eluser]
Thanks for the reply. I think with that routing added it only allows for the use of one controller, and just routes everything through that one controller. I need to still be able to use another controller, so that I have a separate one for the admin pages. What I could do is create a controller for every page, but it doesn't seem as efficient as it could be. Maybe it's the only way though. Anyone have any ideas?
#6

[eluser]Aken[/eluser]
You can create as specific or generic of routes as you need. So if you have a few controllers that are standard, you can specify them before your generic route. Kinda like:
Code:
$route['(login|logout|account|settings)'] = '$1';
$route['(:any)'] = 'default/$1';
That solution is okay if you don't have a ton of specific controllers you don't want to overwrite, but not really suitable for a full website's worth of controllers. So depending on how many you have, you may want to do change your URLs slightly, and just make sure your visitors are redirected to the new ones properly through .htaccess.




Theme © iAndrew 2016 - Forum software by © MyBB