Code Igniter: default controller for unrouted requests - php

I have a CI setup where a URL may invoke a specific controller OR should be forwarded to a catch-all controller where no such controller exists. Sort of like default in a switch statement. Examples:
domain/real-controller //<-- handled by controllers/Real-controller.php
domain/another-real-controller //<-- controllers/Another-real-controller.php
domain/foobar //<-- no such controller; forwarded to a catch-all
I'm aware of rerouting, but I can't do
$route['(:any)'] = 'catchall_controller'
as this will (presumably) block reqeusts to legitimate controllers.
I could presumably do something hacky with 404 handling, but I wondered if there was a better way. Anyone know one?

Since this controller is a "catch all", then it is pretty much doing what a 404 page would do. In that case, you can do this in your routes:
$route['default_controller'] = 'welcome';
$route['404_override'] = 'catchall_controller';
$route['translate_uri_dashes'] = TRUE;

You CAN use $route['(:any)'] = 'catchall_controller' but you MUST put it on the end of your routes.php file :).
Therefore, every other router/controller can be fulfilled before going to the last line that has your catchall_controller.

In codeigniter 2 the (:any) works for all parameters, but in codeigniter 3 this is changed. Change your route to:
$route['(.*)'] = 'catchall_controller';

Related

Purpose of routes.php in codeigniter

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

Special Case with CodeIgniter Routing

So I've got a mostly static site. My default controller handles most views in it's index action, simply passing the $this->uri->segment(1) through to my template.
$route['default_controller'] = 'master';
$route['(:any)'] = 'master';
$route['404_override'] = '';
But now I'm implementing a new controller that I would like to have default behavior. And I don't want to be sullying up my routes config with superfluous routes for every single action. So how can I say, route anything to the default master controller except for NewController, which you should handle normally.
Use the 404_override route location to handle your wildcard pages. That way you won't need to define every existing controller/method that you create.
Just make sure that the 404_override controller/method then correctly outputs an HTTP 404 header, and any appropriate output for the browser.
Not totally sure what you mean but if I understand you correctly if you add the line
$route['NewController'] = 'NewController';
In before the (:any) route it should load it first.
$route['default_controller'] = 'master';
$route['NewController'] = 'NewController';
$route['(:any)'] = 'master';
$route['404_override'] = '';

CodeIgniter turn off automatic routing?

Is it possible to turn off the automatic routing in CodeIgniter and have it only process requests if a route for that request exists? Thanks.
Keep in mind that Dale's solution:
$route['(:any)'] = "some/default/controller/$1";
only works for one-segment URLs, like:
example.com/foo
but not for:
example.com/foo/bar
You can get around this by using a regular expression instead of the CI wildcard. And by routing to a non-existing class drops a show_404() indeed:
$route['(.*)'] = "none";
AFAIK you can't turn off CI's automatic routing, but there is a work around:
// you specific routes
$route['admin/(:any)'] = "admin/$1";
$route['search/(:any)'] = "search/$1";
// the catch all route
$route['(:any)'] = "some/default/controller/$1";
Which doesn't actually turn off CI's routing but routes all unmatched uri's to the default controller.
Alternatively you could route to a non-existent controller which I believe will throw the in built 404 error
Well, another solution could be extends the Router.
Create a class name MY_Route at /application/core/MY_Router declared as class MY_Router extends CI_Router.
You could override the method _set_routing():
This function determines what should be served based on the URI request, as well as any "routes" that have been set in the routing config file.
It should be more complex, but at least can guide you to another solution.
Codeigniter 4 solution:
app/config/Routes.php at line 24 set value true to false
$routes->setAutoRoute(false);
No, it's not possible turn off the automatic routing convention in CodeIgniter as far as I know, but you can put entries in your .htaccess file to redirect de default routes to the routes you've created.

codeigniter routes issue for main url

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.

CodeIgniter __remap() routing more than one controller

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

Categories