Special Case with CodeIgniter Routing - php

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'] = '';

Related

how to define route in codeigniter i am trying but it not working

$route['default_controller'] = 'Student';
$route['admin'] = 'index.php/Admin_depart/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I am fairly new to CodeIgniter and finishing up my first project. However, before I put it up on my hosting site I would like to clean up the URL's using the routes.php file that CodeIgniter provides in the config folder. but it is not working it work correctly when i write same url i the default_controller
The index.php is causing the issue.
Route destinations must be defined only as a controller/method pair. So in your case the correct route would be:
$route['admin'] = 'Admin_depart/index';
anyone browsing to yoursite.com/admin would be served whatever is in your Admin_depart controller, index method (just as if he/she browsed directly to yoursite.com/admin_depart/index;

Code Igniter: default controller for unrouted requests

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';

CodeIgniter : page not found issue appear when i call default controller in Php?

Structure of my controller directory:
--Controller
--frontend
--user_controller
When i call default controller it says 404 page not found
Route i have set :
$route['default_controller'] = "frontend/user_controller";
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
And when i run http://localhost/example/frontend/user_controller then it works.
So but didn't work this url : http://localhost/example
Please try with: move default controller at bottom
$route['admin_panel/site'] = "site";
$route['admin_panel'] = "backend/admin_controller/dashboard";
$route['admin_panel/login'] = "backend/admin_controller/index";
$route['admin_panel/(:any)'] = "backend/admin_controller/$1";
$route['admin_panel/(:any)/(.*)'] = "backend/admin_controller/$1/$2";
$route['default_controller'] = "frontend/user_controller";
Codeigniter will not allow default controller to be in a sub directory.
You have to rewrite it as
$route['default_controller'] = "user_controller";
And place that directly in the controller folder
You can refer this thread to avoid this. But I didn't tested it.
It is better to avoid the core functionalities as it may affect the updates.
I have not tested this but potentially you could use this to get around the sub-folder?
$route['frontend'] = 'frontend/user_controller';
$route['default_controller'] = 'frontend';
need to make sure there is a 'public function index()' in there though as you aren't specifying the method to load...

Codeigniter Routing

I'm trying to write an application that takes a parameter from the url and supplies it to the index function in the default controller. Then decides what to load depending on this parameter either a default home page or a custom page.
$route['(eventguide/:any)'] = 'eventguide';
This code works but only if I have the controller in the url like so:
example.com/eventguide/(parameter)
I don't want to include the controller name. So i'm not exactly sure how to route this.
Ideally the url would look like example.com/(parameter), is this possible?
Yes, you're almost there:
$route['(:any)'] = "eventguide/index/$1";
And in your index() method you fetch the parameter:
public function index($parameter = null){
}
$parameter will now contain anything caught by the :any shortcut, which should be equivalent to (\w)+ IIRC
Since this is a catch-all route, be careful to put any other custom route you want before it, otherwise they will never be reached.
For ex., if you have a controller "admin", your route files should be;:
$route['admin'] = "admin";
$route['(:any)'] = "eventguide/index/$1";

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.

Categories