Welcome Guest, Not a member yet? Register   Sign In
How to make request to non-existent controller mapped to given controller?
#1

[eluser]Morgan Cheng[/eluser]
I noticed that, in twitter, each person's home page has only one segment in path, such as "http://twitter.com/morgancheng". I'm wondering how to implement this in CodeIgniter. Obviously, I cannot make controller for each user. All I want is to map non-existent controller to a given controller. In that controller I will parse the URL path and fetch data for given user.

How to do it in CodeIgniter?
#2

[eluser]WanWizard[/eluser]
You do that using routes. There's an excellent piece about it in the manual.

Issue with this URL is that you'll have to define routes for all controllers that do exist, before you can define a route that says 'for everything else, route to this controller'.
You could modify the routes.php config file so that it checks if the controller exists, and if so, define a route to that controller. If not, route to your users controller.

An easier way of doing things is to use http://website/users/morgancheng, and use the _remap() method to capture the username.
#3

[eluser]mattpointblank[/eluser]
In application/config/routes.php:

Code:
$route['(:any)'] = "usercontroller/$1";

This will send any link like yoursite.com/parameter to usercontroller/parameter. The drawback of this is that for every 'static' controller you create, you'll have to add it as a route.
#4

[eluser]Morgan Cheng[/eluser]
@mattpointblank, it works with a "_remap" method defined in "usercontroller" to handle any kind of URL.

The codeigniter document doesn't mention sequence of routes. It seems that the sequence doesn't matter.

Code:
$route['(:any)'] = "test/$1";
$route['timeline'] = "timeline"

and

Code:
$route['(:any)'] = "test/$1";
$route['timeline'] = "timeline"

have same effect.

[quote author="mattpointblank" date="1272639695"]In application/config/routes.php:

Code:
$route['(:any)'] = "usercontroller/$1";

This will send any link like yoursite.com/parameter to usercontroller/parameter. The drawback of this is that for every 'static' controller you create, you'll have to add it as a route.[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB