Azad Chouhan
Azad Chouhan

Reputation: 290

Codeigniter controller not found

I have create a controller in codeigniter. The problem is that when I hit the controller name and function name in url then Its give me error that 404 not found. I am new to codeigniter and I did not understand where I am going wrong

Here is my controller

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller{

function __construct(){
parent::__construct();
}

function you(){
$this->load->view('home_view');
}

}
?>

and my view is like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>My First CodeIgniter Practice</title>
 </head>
 <body>
 <h1> HELLO CI Buddy...!</h1>
 </body>
 </html>

I am creating my first project on codeigniter and I stuck in this problem.I already try most of the solution from the stackoverflow and after that I asking this question. So Its my request not to add this question to duplicate. I want solution for my problem. any help is appreciable

Thanks

Upvotes: 7

Views: 34631

Answers (8)

Janvier123
Janvier123

Reputation: 11

If you have your CI in a subfolder, let say c:/xampp/htdocs/somemap/ci Change your .htaccess to

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /ci/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ci/index.php [L]
</IfModule>

Please note the /ci/

Upvotes: 0

Abel Callejo
Abel Callejo

Reputation: 14919

There are a lot of possible causes as to why it is happening. In my case, the cause is about the use of underscores in naming a controller e.g. My_Custom_Controller

routes.php (problematic)

$route['translate_uri_dashes'] = FALSE;

$route['my-custom-path'] = 'my_Custom_Controller/landing';

So, I was able to fix it by renaming the controller into Mycustomcontroller then that was the only time it worked.

routes.php (functional)

$route['translate_uri_dashes'] = FALSE;

$route['my-custom-path'] = 'mycustomcontroller/landing';

The naming doesn't follow the suggested naming convention of CodeIgniter but it definitely solved the issue for me.

Upvotes: 0

GhusiMushi
GhusiMushi

Reputation: 33

While thinking about this problem I wonder this might happened to starters like me Suppose we have the project in a folder named codeigniter. so when typing the url we type something like this localhost/codeigniter/ and it shows the welcome message.But it automatically adds index.php defined in config.php (line 38) after the url like this localhost/codeigniter/index.php. So if you don't have mod_rewrite written in .htaccess file don't worry, try this url

localhost/codeigniter/index.php/home/you

in your case home.php is the controller file and you is the method here

Upvotes: 3

Yadhu Babu
Yadhu Babu

Reputation: 1521

Your issue may be due to any of the following reason

  • First letter of your controller file name should be in capital letter ie Home.php .
  • Please check your home page file name
  • Use the URL localhost/w3crud/index.php/home/you if the issue is in your url then edit your .htaccess file

Upvotes: 4

Rajan Acharya
Rajan Acharya

Reputation: 99

Are you running your project on a live server? If yes, then make sure the filename of your controller starts with a upper case letter. It might seem like a silly answer but mind it that I myself spent more than five hours trying to fix the same problem.

Upvotes: 1

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

Try as following

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller{

   public function __construct(){
      parent::__construct();
    }

    public function index(){
      $this->load->view('home_view');
    }

}
?>

and make sure your home_view.php file exist with your code in application/views directory. Now you may run URL http://localhost/w3crud/home/index to see the desired output

Upvotes: 0

user5009491
user5009491

Reputation:

I think there might be problem with .htaccess file. Do you have .htaccess file in your main project directory and is that written like following ?? If not make that so..

 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.+)$ index.php/$1 [L]

Upvotes: 7

Ajay Gokhale
Ajay Gokhale

Reputation: 37

Please try with following

a) Create member function index() in controller and check

b) Which url yor are checking?

b.1) http://localhost/yourproject/index.php/home/you

(localhot: is your server url, yourproject: your project folder, home = your controller file i.e. Home.php and you: your member function.

Upvotes: 0

Related Questions