Routing issue with 404_override - codeigniter

I'm having an issue with 404_override in CI 2.02. Here is my default controller and override:
$route['default_controller'] = "home/index_controller";
$route['404_override'] = "misc/site_map";
This line uncommented gives me this error:
Unable to load your default controller. Please make sure the
controller specified in your Routes.php file is valid.
But commented, I get a 404 error. So something in the override is causing the problem. I just don't know what. I've tried various MY_Router files and they don't help. Anyone have any suggestions about how I can fix this?
I've eliminated my .htaccess file as the problem by deleting it from the server and then trying to access a controller that doesn't exist like this: http://domain.com/index.php/doesnotexist. I still get the error.

For anyone else coming here with this issue, this is/was a bug in CodeIgniter itself to do with 404 pages not being able to be included in subfolders.
Bug Discussion Including Fix on GitHub

You have forgotten to add the default controller that will be used when you first load your website, in your routes.php you need to add this, it is required which is why you're seeing the errors being thrown.
$route['default_controller'] = 'index'; // this will need to be changed to your default controller
$route['404_override'] = 'misc/site_map';
EDIT:
Okay, you must make sure you enter the correct controller name you wish to load as the default, for example you have home.php in your controllers folder, you must enter home as the default. Optionally, you can define segments that are functions in your home.php class file, e.g. home/function_name.

Related

Redirection issue in Codeigniter4

I have done admin controller and put that in a sub folder named 'Admin'
Controller
Admin
-login.php
Now I want to fetch that by router file where I wrote this
$routes->get('admin', 'Admin/Login::index');
But it is showing me "Not found" error and redirects to "http://localhost/admin".
Could there be some .htaccess issue?
replace this
$routes->get('admin', 'Admin/Login::index');
with
$routes->get('admin', 'Admin\Login::index');
also make sure you add namespace in your login.php
namespace App\Controllers\Admin;
If you keep CI4's directory structure intact you could in fact use sub-folders for Controllers, Models, Views, etc.
For example app/Controllers/Admin/Login.php is a valid place to put a Controller class. Make sure to add the appropriate namespace in Login.php - namespace App\Controllers\Admin; Also in routes - $routes->get('admin', 'App\Controllers\Admin\Login::index'); It is quite possible to work without the prefix of App\Controllers, but I never extensively tested it and I think there was a problem in some versions of CI4 before.
Another issue could be your app/Config/App.php class. If you did not change anything in your .htaccess file (the one in public directory!), $baseURL should be set to your public directory address - http://localhost/myproject/public/ . Or if you wish to make it easier - set up virtual hosts.
Just a thing to add - get() method in $routes allow only GET requests, meaning if you are trying to POST something (or use any other HTTP request method) it will fail and redirect.

Default controller (under modules) not loading under HMVC, CodeIgniter, throws 404 err

Just migrated my CI from localhost to live. Everything works fine on localhost with same settings.
Here're the folder/file structure and contents of routes.php
-application
--controllers
---Home.php
--modules
---specs
----controllers
-----Specs.php
----models
----views
$route['default_controller'] = 'Specs/index';
//$route['default_controller'] = 'Home/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
Both Specs and Home controllers have index method. When I load the home page url with Specs/index as default controller, I get 404 error. But if I change the default controller to Home/index, it loads just fine. Strange. Everything else works just fine on whole site. Can't figure out.
The website is live but I'm not sure if providing url is allowed, so I'm including spaces in it. It's unspecs dott comm. Admin/Mods may please remove if url isn't allowed.
Thanks for reading.
I think you are doing mistake in routing, for HMVC it should be like that -
$route['default_controller'] = '<folder name>/<controller name>/<function name>';
$route['default_controller'] = 'specs/Specs/index';
Please follow this, hope it will work fine.
Try this https://stackoverflow.com/questions/6529026/codeigniter... But better use standart folder for default controller /application/controllers

hmvc codeigniter uri routing

I have and CI hmvc structure as below:
application
config
config.php
routes.php
...
modules
login
personal
config
config.php
routes.php
controllers
libraries
models
views
HMVC works fine. So, here's my problem: Inside /modules/personal/config/routes.php i have this:
$route['personal/empleados/actualizar.legajo'] = 'personal/empleados/actualizar_legajo/';
$route['personal/empleados/nuevo-cargo'] = 'personal/empleados/nuevo_cargo/';
But it doen't work. The file is loaded but routing doesn't work.
If i cut and copy these two lines and paste them inside: /application/config/routes.php, works.
Can anybody tell me why ? Because i have several modules and i would like to do this inside each modules/module-name/config/config.php
Thanx, in advance.
Have you tried to set the default controller in each module ?
You may also need to add the folder path.
modules/personal/config/routes.php
$route['default_controller'] = 'personal';
// OR //
$route['default_controller'] = 'personal/personal';
Thanx for your answer.
I set my default controller as you tell but it fails anyway.
On the other hand i found a solution (?) as follow on modules/module-name/config/routes.php
$route['personal/empleados/nuevo.cargo'] = 'empleados/nuevo_cargo/';
and... IT WORKS. I don't know why. But it works.

CodeIgniter redirect not working on a real Server

I'm having a problem with CI 2.1.3 redirect function.
Everytime I call redirect, it shows a white-blank page. In fact, it works well on my localhost, the problem just occurs on my real server (with CentOS 5 installed).
This is how I call the redirects :
redirect('frontend/article/index');
or
redirect(base_url('articles.html'));
I did add a route in config/routes.php
$route['articles.html'] = 'frontend/article/index';
with : frontend is module, article is controller, and index is action (I'm using wiredesignz's HMVC module extension)
How could I fix it? And what is the problem here?
Thanks in advance!
UPDATE
I replaced CI redirect function by calling :
header("Location: http://example.com");
but it didn't work too.
So I created a file named info.php and uploaded it to my server. Here's the content:
<?php
phpinfo();
?>
When I type in the address bar : http://example.com/info.php, it shows like in the image.
Why was there a ">" character? Was it the problem causing redirect not working?
Firstly, make sure error reporting is enabled, or try placing the following at the top of your index.php.
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
If this doesn't point you in the right direction make sure you don't have any output echo, print_r, print, dump, etc before your call to redirect() method.
Another common cause or problems when moving to a new environment is white space. Check that your files don't have any whitespace at the bottom of them.
if you are defining .html in the config.php as the file ext. you do not need to postfix the route with it.
$route['articles'] //instead of $route['articles.html']
Also you need to remove the base_url() from the redirect cos that is not needed.
redirect('articles'); //should sort it
Hope this sorts ur problems.
EDIT
If this is still not working after attempting these changes, it will most likely be a problem in the controllers. If this is the case you may need to turn on error reporting in your index.php file to find out exactly where the problem is occurring.

Request not routed on CodeIgniter

I've got a working CodeIgniter website on my machine. However, when I move it to the live server, it says that it is unable to determine what should be displayed. I checked the paths, base URL and .htaccess and everything seems to be correct. How can I find out what the problem is?
Edit: This is the content of routes.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "";
$route['404_override'] = '';
The error message does suggest that CI is attempting to fall back to the default controller which you have not specified. Assuming that the controller you are calling does exist this suggests that the problem might be to do with the server configuration and the way it is handling the URLs.
Try changing the URI Protocol in the config.php file (line 47). The default is 'AUTO' which works most of the time, but I have known servers that didn't like this. The comments in the file suggest various values you could try.
From the code supplied above, there is no default controller. CI requires a default controller is set, hence the error message.
If you have set one, then the problem isn't related to the above code.
You should setup your default controller in routes.php, for example
$route['default_controller'] = "home"; // home is the name of the controller

Resources