Instead of default controller how can I call directly one controller? My requirement is: I need to call one controller separately for loading my view file. When I try the standard way of calling it I get redirecting.
open the file in the config folder name route.php and replace
$route['default_controller'] = 'Welcome';
to
$route['default_controller'] = 'your controller';
for more read this
http://w3code.in/2015/10/codeigniter-installation-beginner-guide/
Related
I am implementing HMVC in codeignter3. I have added all the required file in core and third_party folder.
I have also created modules folder. Inside module I have created frontend folder Inside frontend folder I have created One folder test inside that I have created 3 Folder controllers, models and view. In controllers folder I have created Test.php and in models model created with name Test_model.php and in views one file created index.php
My controller code is
<?php
class Test extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('test_model');
}
public function index()
{
$data['main_content'] = 'home/index';
$this->load->view('front/layout', $data);
}
}
?>
inside application/config/routes.php
$route['test'] = "frontend/test/Test/index";
When I am accessing it through localhost/myprojectfoldername/test I am getting 404
yes, this gives you an error. because you have created modules folder inside the module folder.
you have to create modules folder in your application folder not to the inside of another folder the path is defined in mx_loader application>modules so the code is not fined the class.
change its directory and refresh it so the error will remove.
further help uses these links:
http://www.dcaulfield.com/how-to-install-codeigniter-hmvc/
Or you can directly download from my gmail drive :
https://drive.google.com/open?id=1Viyo7CQcjJNkBv5ahyiOs5RwwXiOVopg
afer downlad and set up, hit url:
http://localhost/hmvc/home
Use like this:
$route['some-route'] = "yourcontroller/yourmethod";
or in other words:
$route['user'] = 'user/login';
One thing about HMVC Pattern in Codeigniter is, you need to follow the folder structure very well.
In your description you said you have created a Test.php file in your controller which resides in a folder called "frontend" which is in another folder "test" in your modules folder too.
Now the trick here is, If your controller name (Test.php) is equal to the folder name (test), you can simply call it like this:
$route['test'] = "frontend/test/index";
Instead of this:
$route['test'] = "frontend/test/Test/index";
On the other side let's say you have created another file( or controller) in the text folder called User.php then you can set your route as:
$route['test/user'] = "frontend/test/user/index";
Then again in your route code example I noticed you tried to use the Uppercase for the "Test", it does not really matter you can just use a lowercase "test" instead:
$route['test'] = "frontend/test/test/index"; But note this was just to explain whether it is case-sensitive or not.
Try your hands and it and let's see the outcome
I have set 404_override in route.php so that when there is no correspondent controller it will redirect to my error page
But my question is if there is no function inside the controller it is not redirecting properly
example: http:example.com/search/function_name
here search controller is there but the function_name not exist, how I can redirect to error page in this scenario ?
Your help will be really appreciate!
If you have set up the 404_override properly it will work as intended. You need to pass the controller method in which you are showing your custom 404 page else it would not work.
Create 'index' function in your error controller. Then you do not have to mention the function name in 404_override.
I want the following to happen
The url something.com/why-us to go to content controller
The url something.com/nepal to go to location controller
How to manage the above in CI routes
Apply the below coding in routes.php in config folder
$route['why-us'] = "content_controller";
$route['nepal'] = "location_controller";
use your desire controller
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.
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