How to set default page in codeigniter - php

I have set routes in routes.php for my default page but it shows a 404 error when opening localhost/mywebsite
$route['default_controller'] = "page";
$route['404_override'] = 'page';
I have set the above code in routes.php which gives a 404 error. The page controller has been created.

You need to make sure following things:
Webserver rewrite module is installed
htaccess file is properly configured
index function is present in page controller

You configured default controller & 404_override URL with the same value.
When ever you access to your web site automatically open page controller index function
ie ur_project_url/page/index
OR
ur_project_url/index.php/page/index
when ever you can get the 404 error also open same page controller index function
ie ur_project_url/page/index
OR
ur_project_url/index.php/page/index
please check your controllers and function and routes ,htacess file
please remove your url index.php using htacess
in config folder $config['log_threshold'] = 0; to $config['log_threshold'] = 1; check log files

Related

CodeIgniter doesn't load default controller on '/'

I'm using CodeIgniter with Nginx.
I have just upgraded from CodeIgniter 2.2.6 to 3.1.4, following all the changelogs and upgrade instruction. Everything works perfectly but the default controller, i.e. http://francescoruvolo.it (or even http://francescoruvolo.it/index.php) shows a 404 page but all other routing rules works and I'm able to load the other controllers.
Here is my routes.php:
$route['(it|en)/contact/check_form'] = "Email/validate_form/$1";
$route['(it|en)/contact'] = "Email/show_form/$1";
$route['(it|en)/(:any)'] = "Pages/show/$1/$2";
$route['(it|en)'] = "Pages/show/$1";
$route['default_controller'] = "Pages/show/";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I have also tried to comment all routes, but still the default one is ignored. Still, if I go http://francescoruvolo.it/Pages/show or either http://francescoruvolo.it/en, controllers are loaded just fine and it works. Many people had similar problems because they missed the part about enforcing the capitalized name for classes, but this is not the case as my controllers' name have been already fixed.
These are the parameters in config.php:
$config['base_url'] = 'http://francescoruvolo.it/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
And Nginx looks like it is properly forwarding all requests to index.php, in fact I don't need to specify it when loading controllers explicitly. Anyway, these lines are part of my Nginx configuration for the host:
location / {
try_files $uri $uri/ /index.php;
}
What I don't get is that I'm actually able to get two different kind of 404 pages. For example, if I go to http://francescoruvolo.it/nonexistent.php Nginx correctly serves me a 404 page. At the same time, if I do not specify any path or explicitly specify index.php I get a 404 "well formatted", which means that the index.php from CodeIgniter has been actually run, but it failed to load the controller.
What am I missing? What else can I check?
Solved. Apparently, I had an extra (unneeded) / at the end of my default route.
This was WRONG:
$route['default_controller'] = "Pages/show/";
This is CORRECT:
$route['default_controller'] = "Pages/show";
Still, I don't quite get why. In fact, if I explicitly load the controller, it works even with the extra slash.
If anyone explains this, I'll gladly accept his answer. :)

Codeigniter ignoring 'default_controller' route

I've got a Codeigniter 3 site where all my routes work except my home page route, which is like so:
<?php
$route['default_controller'] = "front/homepage";
?>
If I create a new route as below I can access it as "/home" but nothing works for index. I get an error in my log as 404 Page Not Found: /index, in my config i have index set to blank.
<?php
$route['home'] = "front/homepage";
?>
Any ideas?
Thanks
This is no-longer supported in Codeigniter since V2 due to it being a bug in the routing logic.
http://www.codeigniter.com/userguide3/installation/upgrade_300.html#directories-and-default-controller-404-override

how do I configure CodeIgniter to open the a controller that is in another folder but with only domain name

Ok, so I have a page 'example.com' and I have a folder that contains all my code that I have been debugin in my localhost.
So my localhost route is localhost/sistem but I want to upload my folder to my domain server at 'example.com' but the route would then be example.com/sistem
I want to make a route to not show 'sistem' at all; that when I access 'example.com' I directly go into sistem without sistem apearring in the URL
example.com /*server*/
sistem/ /*root folder*/
login.php /*which has the index*/
other.php
other2.php
what I want is the URL to be example.com but to show login.php's index function.
I know i use
$route['default_controller'] = "login”;
but that opens after I access 'example.com/sistem'
Add it in your route configuration:
$route['default_controller'] = "sistem/login”
$route['sistem/'] = FALSE; // Show 404
$route['sistem/(:any)'] = FALSE; // Show 404
$route['(:any)'] = 'sistem/$1';
$route['(:any)/(:any)'] = 'sistem/$1/$2';

CodeIgniter: Include a controller outside the application environment

So basically, I would like to load a CodeIgniter controller contents outside CodeIgniter's environment, that is, from a traditional PHP page.
I have tried the following but it doesn't suit my requirements:
<?php
require('/application/controller/error?type=not_found');
?>
It gives the following error:
Message: require(/application/controller/error?type=not_found): failed to open stream: No such file or directory
To clarify, I'm trying to change the default /application/errors/error_404.php page contents to this so it will display the custom HTTP 404 error page I've built in the "error" controller & view.
The problem is, The pages under /application/errors/ are treated as normal PHP pages by CodeIgniter which means I can't use $this->load-view('error'); inside the error_404.php page.
I would also appreciate other suggestions for ways to create custom error pages in CodeIgniter.
You should set the 404 page in the route.php page in the application/config folder:
$route['404_override'] = 'your-error-controller/your-error-view';
This route will tell the Router what URI segments to use if those provided in the URL cannot be matched to a valid route.
I might be confused at your question but the routes.php gives you a place where you can point the 404 page to a custom view.

default_controller (in routes.php) not working in production

Regarding codeigniter routes.php :
We have following entry in C:\wamp\www\application\config\routes.php
$route['default_controller'] = 'latestC';
$route['404_override'] = 'latestC';
and latestC is our default controller. Here default_controller is not working in production. If we remove line $route['404_override'] = 'latestC'; from routes.php, we are not able to reach to home page while hitting main url mozvo.com and its a 404. Basically 404_override is doing job for us instead of default_controller for taking to homepage on hitting mozvo.com. Requests are routing to home page by 404_override controller.
But in localhost, it works perfectly. In localhost if we remove 404_controller, default_controller takes care of main url (mozvo.com, here localhost ) and others non supported urls are 404 which is correct. But in production default_controller is not taking to homepage(mozvo.com) properly so we are forced to use 404_override to take default request to homepage.
Additional Info - Entries in C:\wamp\www\application\config\config.php
$config['base_url'] = 'http://mozvo.com/';
$config['index_page'] = '';
I bet you the issue is due to case-sensitivity on files. Your local host is on WAMP - which windows does does not care about file cases.
i.e. latestC.php = latestc.php = LASTESTC.php
but on your production server (which I'm guessing is a LAMP) - case sensitivity DOES matter
i.e. latestC.php != latestc.php != LASTESTC.php
All your controllers must be LOWERCASE for Codeigniter. So change your routes to
$route['default_controller'] = 'latestc'; // all lowercase
$route['404_override'] = 'latestc'; //all lowercase
and make sure all your files are all lowercase

Categories