Welcome Guest, Not a member yet? Register   Sign In
How do I make a defaulr controller with wildcard 1st URI Segment
#1

[eluser]Brennan Novak[/eluser]
Ok so what I'm trying to do is make my default controller instead of "welcome" be

"site"

I'd like to make

http://domain.com/site

eliminate the "site" part from the first URI segment so it redirects back to just http://domain.com

Whenever I alter the index() function in Site controller it gets caught in a redirect loop. Is there a way to do this?

Additionally make the 1st URI segment wildcard so it looks for a controller with the name, then not redirects through the Site controller.

I read this doc about routing but it didn't seem to help.

http://ellislab.com/codeigniter/user-gui...uting.html

The reason for this being cleaner URLS and less controllers for adding simple static-ish pages to a CI application. So instead of

http://domain.com/site/about

You just get

http://domain.com/about

Maybe this is a bit complex of task to achieve, but if not I'd love some with how to approach this as I think this would really useful.
#2

[eluser]must[/eluser]
I think you can get this working by adding
Code:
$route['(:any)'] = "site/$1";
but this would make you obligated to use one controller (named site in this case).

edit: it may be a way to have other controllers with this method by adding something like:
Code:
$route['controller_name/(:any)'] = "controller_name/$1";
     $route['(:any)'] = "site/$1";
not sure if this works, try testing it.
#3

[eluser]Brennan Novak[/eluser]
Yah, I tried that. But, like you said it makes be forced to use that one controller. I was hoping to make it search for other controllers and *if none exist* default to the "site" controller. Kinda strange. But makes sense yes?
#4

[eluser]must[/eluser]
there's another way but you would have to do it manually for each method of the site controller
example :
Code:
//(:any) if you wanna be able to pass data to the method trough segments
     $route['method_name/(:any)'] = 'site/method_name/$1';
     $route['method_name2/(:any)'] = 'site/method_name2/$1';
#5

[eluser]Brennan Novak[/eluser]
Ahh schucks. The goal is to allow for user profile pages to be a simple URL like is common on many sites (GitHub, Facebook, Twitter, etc...)

http://domain.com/username

But also still allow room for other controllers that have different functionality rather than routing everything through one single controller. Strange that this isn't easily possible with robust framework like CI. I've always been able to achieve with mod_rewrite + raw PHP
#6

[eluser]must[/eluser]
well, any thing is possible but I think that you would have to hack some core classes or there may be a way with hooks.

-Maybe you can make a pre_controller hook to check for the first segment value in the user database, if it exists call the site controller (or the controller which will handle the username profile) else continue, but I don't know how to change the controller that's going to be executed + if this work you have to restrict pseudos that matches any of your controllers names + you would have a query that runs through every page.
-Or you can apply the same method (assuming that it works) and do the opposite : checking if the first segment matches one of your controllers, if not then send it to the site (profile) controller and that controller will send an 404 error if user wasn't found (this would probably be the better way and you would also restrict pseudo that matches controller names or else they won't have a profile page).
-Another way with hooks is to do a post_controller hook that checks if the controller couldn't be executed (404 error) then execute the site controller (this would be an even better way xD).

-There may be a way without hooks too by extending the routing library (MY_router) and changing the way it behaves depending on the URI entered (kind of the first method that i mentioned with hooks) but then you would have to dig in the router library indestand how it work and figure out how to change it behavior.

All of these methods either depend on knowing how to retrieve the status of the executing controller or/and how to make CI execute the controller that you want. I have never used hooks nor do i know how they really work (internally) so I'd recommended that you dig a little bit in the core classes to figure out how they work / what methods and variables available for the hooks.

In Conclusion there definitely are a lot of ways of doing what you wanna do (some more complex/better than others).

ps: Maybe they will add an easier way to do this in CI 2.0 Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB