I want to maintain a complex URL structure while routing. I could not write routing for the following requirement.
If the URL comes with http://localhost/api/cals/func/id I want to route it to http://localhost/api/cals/func/id, otherwise I want to route the URL to http://localhost/home/.
I tried this but it is not working in all use cases.
$route['api/(:any)'] = 'api/(:any)';
$route['(:any)'] = 'home/index/';
Why is this so?
This may help you;
You must use $1 for it.
$route['default_controller'] = 'home/index/';
$route['api/(:any)'] = 'api/$1';
$route['(:any)'] = 'home/index/';
Related
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 have defined a new route on routes.php but it have problem.
$route['default_controller'] = "index";
$route['404_override'] = '';
$route['(:any)'] = "oyna/oyun/$1";
I want to redirect /2012.htm to oyna/oyun/2012.htm and I can but it create a new problem. I can't reach my other controller if I don't define as below:
$route['default_controller'] = "index";
$route['404_override'] = '';
$route['admin/(:any)/(:any)'] = 'admin/$1/$2';
$route['admin/(:any)'] = 'admin/$1';
$route['kategori/(:any)'] = "oyna/kategori/$1";
$route['(:any)'] = "oyna/oyun/$1";
If i don't define any controllers on routes.php like above I can't reach that.
What i need to do for solve?
I'm not sure, but try to replace $route['(:any)'] = "oyna/oyun/$1"; with $route['(:num).htm'] = "oyna/oyun/$1.htm";
Or better: $route['(\d+).htm'] = "oyna/oyun/$1.htm";
CodeIgniter's routes are a little funny, but once you understand how they're processed, it makes perfect sense.
Since routes use regular expression matching, you can't just have something super generic and expect everything else to work, because it's going to look at routes before simply routing to the controller/method implied by the URL.
If you want to match URLs such as http://domain.tld/2njkf4r AND http://domain.tld/pages/about then you will have to create more specific rules to handle "exceptions" to the very general rule that will match the first case.
You are correct that it won't work unless you define the other routes, because with only $route['(:any)'] as a route, EVERY request will match that. That route has to be your absolute last route. It's a pain in the ass, but necessary, because of the way they process routes.
My all links have .htm at end's. And this is the solve:
$route['(:any).htm'] = "oyna/oyun/$1";
Because my controllers haven't got .htm at the end.
I want to route so that the "domain" part dissapears,
http://www.domaininfo.za/domain/google.com
to something like this
http://www.domaininfo.za/google.com
I have managed to remove the welcome part but how do i remove domain?
my route file:
$route['default_controller'] = "welcome";
$route['404_override'] = '/';
$route['(:any)'] = "welcome/domain/$1";
Not sure if this is what you're asking, but does this work for you?
$route['(:any)'] = "domain/$1";
Your routing is dangerous...I would put a regex in the route key to recognize web domains, then route it to welcome/domain/$1. Right now you are routing all values in the controller space to welcome/domain/$1. Mod-rewrite or using regex in your router would be best.
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
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";