I am working on a codeigniter app and am having some trouble wrapping my head around a routing issue. Basically I would like all routes to map to an specific controller action by default, but I would also like to able to specify an array of routes (or ideally initial url segments) which shouldn't follow this pattern.
By way of example:
If I enter domain.com/username it maps to domain.com/controller/method/show/username
If I enter domain.com/account it maps to domain.com/account
Any help very gratefully received!
James
Open config/routes.php and add the following:
$route['(:any)'] = "controller/method/show/$1";
Please see the link below for more routing concepts.
http://codeigniter.com/user_guide/general/routing.html
Routes will run in the order they are defined. So in your routes file, put the routes for other controllers you still want to work above your catch-all for usernames:
$route['default_controller'] = 'home'; //so root url still works
$route['accounts'] = "accounts";
$route['accounts/(:any)'] = "accounts/$1";
...
$route['(:any)'] = "controller/method/show/$1";
Related
My application URL "localhost/crdlabs/PHP" goes to "localhost/crdlabs/home/display/PHP" which is done using route.php. The rule is as follows.
$route['(:any)'] = "home/display/$1";
Now that I have another controller class called displayarticles(). The current URL for this class is "localhost/crdlabs/displayarticles/article/learning-coding". I understand that I cannot use the routing above for the new controller. How to set a routing rule for the current one to make the URL look like "localhost/crdlabs/learning-coding".
Note : learning-coding part is dynamically set which means there are several different articles that should go to the same controller.
Any help/advise please.
The current routing rule.
$route['(:any)'] = "home/display/$1";
Will routes anything appearing after localhost/crdlabs/argument to localhost/crdlabs/home/display/argument.So the route localhost/crdlabs/learning-coding will be redirected to localhost/crdlabs/home/display/learning-coding.So you can not use like this.
But I suggest you to show your articles
localhost/crdlabs/displayarticles/article/learning-coding
To
localhost/crdlabs/articles/learning-coding
using the following routing rule.
$route['articles/(:any)'] = "displayarticles/article/$1";
Will be best.
I'm new in Codeigniter framework and got routing problem.
I have Main controller and Tournaments controller.
My routes look like this:
$route['default_controller'] = 'main';
$route['main'] = 'main';
$route['tournaments/results/(:num)'] = 'tournaments/results/$1';
When I go to localhost/tournaments/results/1 and then click to link "Main" I got localhost/tournaments/results/main instead of localhost/main
What is wrong with my routes or maybe problem is somewhere else?
Probably your link to 'Main' is wrong, not your routes files. Check your link.
I just want to know the purpose of this on routes.php
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
Let me first take your through routing and then tell you about the meaning of those lines in routes.php.
You should start with URI Routing. In short, what you do with routes is that you map a certain URI with a controller/method/parameter statement.
Examples
See the following examples taken from the user guide:
So, something like example.com/journals can be routed to the blogs controller.
$route['journals'] = "blogs";
Another good example is when you are building a product catalogue and you need example.com/product/some_id to be routed to a controller catalog:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
In the example above, catalog will be the controller, product_lookup_by_id will be the method and $1 is the parameter which is picked up from the URI.
Answer to your question
You have asked:
I just want to know the purpose of this on routes.php
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
The default_controller is quite obvious. This means welcome/index will be loading whenever `example.com/index is being requested.
scaffolding_trigger was deprecated in 1.7 but you can read about it. Scaffolding was a method which you could use to seed data in your database.
$route['default_controller'] = "welcome";
This is the default controller which codeigniter would start with when you didnt specify a controller to the URL.
url:
ip/monitor/index.php
This will trigger the default controller aka welcome.php
url:
ip/monitor/index.php/controller
This will, however, trigger your given controller and not the default one
This is mostly used to asign a index page as the start page.
I'm not sure about $route['scaffolding_trigger'] = ""; as i never used it. But according to the comments it has been removed in version 2.0
Routing rules are defined in your application/config/routes.php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions.
For more detail please see this link :
http://ellislab.com/codeigniter%20/user-guide/general/routing.html
I'm using codeigniter framework and by default i load my site controller
$route['default_controller'] = "site";
But now i want to setup the routes.php config so i could still access my controllers as before but if controller doesent exists then i'd like to run users controller and check if it's nickname so i can display that users profile.
It's something like on Facebook where you can have www.facebook.com/username and it takes you to your user profile. But i'd like that my other controllers are still accessible www.mysite.com/site, www.mysite.com/site/function, etc
I have tried with :any wildcard but couldnt get it working. I have seen that some solve the problem with regex expression but i'm not good at regex and dont know how to put it together so it would work for me.
Try my way:
First, Set $route for each controller that you have, exmp:
$route['controller_1'] = 'controller_1';
$route['controller_1/(:any)'] = 'controller_1/$1'; //this let you access your method
$route['controller_2'] = 'controller_2';
$route['controller_2/(:any)'] = 'controller_2/$1'; //this let you access your method
Next, use :any
$route['(:any)'] = 'user_controller/$1';
good luck !!!
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}
I am trying to create a URL shortener using CodeIgniter 2.
I have 2 controllers: main and api.
For redirecting a short link through the router, I am using this setting in config/routes:
$route['(.*)'] = "main/$1";
along with a method in the main controller which should work. However, the controllers won't start. Please help me to solve this problem.
You controller "any" isnt called because it falls into that regex, so it's routed to main.
In order to exclude "any" from this rule you need to create a special rule for that, keeping in mind that for CI rules are cascading , so they're executed in the order they're presented
Note: Routes will run in the order
they are defined. Higher routes will
always take precedence over lower
ones.
So, you would have:
// reserved routes must come before custom routes
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['any'] = //your rule here. maybe "any". ?
$route['(.*)'] = "main/$1"; // CI also provides you with `(:any)` rule, that mateches any character.
More on this here: Uri routing