I was using small letter for controller file name before,
however, recently move the file to other server that need to controller name to be start with capital
=========================================
Here is the rename example :
file name: Home.php
$route['default_controller']: "home";
link: http://example.com/index_folder/
==========================================
it show error of
Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.
And when I change the route to
$route['default_controller']: "Home";
it still show the same error,
Only success if I go to
http://example.com/index_folder/Home/
How to fix that? Thanks for helping
Update
Here is the htaccess file, at the root of the project folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The reason of rename is it seems the new server has problem if my Controller is in lower case
Linux is case sensitive, keep the file name and class name same and try again. It will work.
The same code will work fine on windows but give exception on Linux.
As per Documentation,
class Home extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Then save the file to your application/controllers/ directory.
Important:-
The file must be called ‘Home.php’, with a capital ‘H’.
Now visit the your site using a URL similar to this:
example.com/index.php/home/
CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested.
To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'home';
Where ‘home’ is the name of the controller class you want used.
If you now load your main index.php file without specifying any URI segments you’ll see your “Hello World” message by default.
Related
I've created one test.php file in location applications\views\test\test.php with controller in application\controller\Test.php & model in applications\models\Test_model.php.
In my routes.php file, I have added $route['test']='test/view'.
Please note there are some files present already which are working quite fine, one of which is login.php & it has value in routes.php as $route['login']='login/view'. Plus this file does have same model & controller files in likewise location.
When I try to access localhost\test, it gives me 404 error whereas for localhost\login works quite fine.
Can anybody help in routing? I'm new to codeigniter & I could not resolve this issue.
EDIT
htaccess works fine as localhost\login is loading properly along with other few files.
EDIT 2
Test_model.php
<?php
class Test_model extends CI_Model{
public function __construct()
{
$this->load->database();
}
}
?>
test.php
<div>Testing</div>
Test.php
<?php
class Test extends MY_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('test_model');
}
public function view()
{
$this->loadHeader();
$this->load->view('test/test');
$this->loadFooter();
}
}
?>
routes.php
$route['test'] = 'test/view';
EDIT 3
When I try to access localhost\application\controllers\Test.php, it gives me an error that says Class My_Controller not found. However, when I attempt to do it with any other file let's login with with the same location, it gives me the same error.
So I guess, it's able to find the controller because it's giving an error obviously but not able to load anything else.
Is there some sort of config file in which I have to mention every new page I create or something? There has to be something. This is pretty basic & I'm not able to get to the root of it.
EDIT 4
So this is what a problem is. When I copied my test view file to pages folder, it worked. Of course in controller I edited path of the file.
Now the real question is why didn't new folder named test under views work?
Finally, the culprit was this line in routes.php file:
$route['(:any)'] = 'pages/view/$1';
I was adding other route below this so it was getting default behaviour. I put them above this line & it worked like a charm. I never thought this could be the issue.
Line number matters, now.
Create a .htaccess file and paste this code below
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
I'm new to codeigniter and I'm working on a project that is done on this framework. When I try to access a url such as "mysite.com/about", it gives me and an error
404 Not found,
I tried many .htaccess file on wamp and a live host but the result is the same.
I can Access these file by using "mysite.com/index.php/about" but I want to access as this "mysite.com/about" this not only for about page but all so very page that I try to access by using like this "mysite.com/contact-us"
.How do I fix this?
Write this code in your .htaccess file and in wamp server click on rewrite_module
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
IndexIgnore *
</IfModule>
By default, Codeigniter will call the about controller, unless a method is provided in the second uri segment, the index() method will be loaded.
If the index() method doesn't exist, it will 404.
The other way of doing it is to add a line in your application/config/routes.php file. Such as:
$route['about'] = 'main/about';
This means, a request where the url matches /about will map to main/about
Main being the controller and about being the method within that for example.
See this part of the documentation for a more detailed explaination
https://www.codeigniter.com/userguide3/general/routing.html
Hope this helps
Make sure that you have named the Class for your controller as the controllers file name. Then try to access the page as shown in the code below.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pages extends CI_Controller {
function about()
{
$data['title'] = 'About us Page';
$this->load->view('about', $data); //about is in views
}
}
?>
The file name should be pages.php in this case.
To access the about page type in this link mysite.com/index.php?/controllerfolder/pages/about.
If you want clean url and remove index.php? edit your htaccess. Use this link to know how to edit your htaccess
https://www.formget.com/codeigniter-htaccess-remove-index-php/.
Hope this is helpful. Thanks
This is the error that i am getting.
Please make sure the controller specified in your Routes.php file is valid.
Let me explain this.
In my application i have used HMVC. In my application folder there are two folders back-modules and front-modules.in my front-modules folder there is a folder called "home" and it has controllers,modules and views.
controllers->home.php
this is my home.php
class Home extends MX_Controller{
public function __construct(){
parent::__construct();
$this->load->helper(array('form'));
}
public function index(){
$this->template->build('home');
}
}
and this is my routes.php
$route['default_controller'] = "home";
$route['404_override'] = '';
when i try to go to the site, it gives this error
unable to load your default controller on Codeigniter.Please make sure the controller specified in your Routes.php file is valid.
this system works fine on localhost.
Someone please help me in this matter.
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
example.com/class/function/id/
your 'default_controller' should be:
$route['default_controller'] = "home/index";
read official CodeIgniter URI Routing Documentation
Kindly Check .htaccess file exist or not in root place of your website.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
Some times codeigniter won't find the controller.
Go to config > routers.php
and see the value of default_controller
make a controller in controllers folder with the same value.
According to codegniter manual managing multi application
in your index.php file change
$application_folder = 'application';
to
$application_folder = 'application/home';
and make sure the controller file name starts with capital letter
controllers->Home.php
and keep Routes.php configurations
$route['default_controller'] = "home";
I'm not really new with Codeigniter but have been working on started projects so far. Now I'm starting a new small project on my own and I'm kinda lost.
I downloaded codeigniter, configured all the parameters in wamp so I have this base URL: http://local.project
Now I'm trying to build a small admin. I should be able to enter to this admin through http://local.project/admin which should show a login page. I already have a template for this.
The thing is that same 404 error appears. The configuration I have is this:
Inside config folder, routes.php:
$route['default_controller'] = "admin";
$route['404_override'] = '';
$route['admin'] = 'admin';
then on controllers folder, created another folder admin with a file also called admin.php which contains:
<?php
class Admin extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
now, trying to access from the browser I've tried many possible url but I'm still not sure of how it should be>
http://local.project/admin
and other combinations like
http://local.project/admin/index.php
http://local.project/index
http://local.project/index/admin
but always appears error 404 page not found.
So I'm really wondering, what am I doing wrong?
EDIT
this is what .htaccess contains>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You're perfectly reasonable to assume that by defining a default controller it should work in subfolders, but ellislabs doesn't agree. As per the user guide:
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the
URI contains no data, which will be the case when people load your
root URL. In the above example, the "welcome" class would be loaded.
You are encouraged to always have a default route otherwise a 404 page
will appear by default.
The key words there are "if the URI contains no data." "/admin" contains data. If you put your Admin controller in the root controllers folder and put in "local.project/" you'd get the index function of the Admin controller, but in a subfolder, you have to specify the full path, which in this case would be "/admin/admin/index." Putting that into your "admin" route will fix the issue, and then adding another route to take care of any other functions ($route["admin/(:any)'] = 'admin/admin/$1' should do the trick) will fix the rest.
And just so you know, if you ever upload this code to a server running Linux, it'll break because "Admin" and "admin" are two different controllers as far as Linux/Unix are concerned. As a long-time Windows dev, I feel your pain on that one.
Some of the controllers in my CI project are not working. The following is the directory structure:
controllers/student/profile/personal.php
When I call it using the URL: localhost/school/student/profile/personal then it displays 404 error.
Contents of personal.php file:
class Personal extends CI_Controller {
public function index() {
echo "The is Personal Section";
}
}
Contents of .htaccess file are:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA,PT]
I don't know why it is not working. There are other controllers like controllers/student/login.php working ok.
Any Idea?
I don't believe CodeIgniter allows organizing controllers into sub-sub folders by default. Sub-folders yes...deeper than that and you run into issues. There are some solutions out there (better or worse) that allow this functionality. A quick Google search turns this up:
https://degreesofzero.com/article/controllers-in-sub-sub-folders-in-codeigniter.html
Also this:
http://ellislab.com/forums/viewthread/190563/
Codeigniter prior to version 3 models and controllers are named in small caps and the class name within the file is named With a Capital first letter
Example if it is class goat the the file should be goat.php
Codeigniter 3 models and controllers are named just like how they are called with the files Example if it is class goat the the file should be Goat.php
failure to do so in either case depending on your codeigniter version ... will result into a not found error