7

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

Azad Chouhan
  • 270
  • 1
  • 5
  • 20

8 Answers8

7

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]
4

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
Yadhu Babu
  • 1,503
  • 2
  • 13
  • 25
3

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

GhusiMushi
  • 33
  • 3
1

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.

Rajan Acharya
  • 99
  • 6
  • 15
0

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.

  • i try with index function still not found – Azad Chouhan May 09 '16 at 11:52
  • Check one by one 1) Check server is working (http://localhost/ ) 2) Check your project default controller is working (http://localhost/yourproject) If both are working then check your controller file i.e. "Home.php" which is placed on /application/controllers folder. First letter is upper case for new CI – Ajay Gokhale May 09 '16 at 12:11
  • but i dont need index.php in the url – Azad Chouhan May 09 '16 at 13:19
  • This is codeigniter default method. Please check online doc: http://www.codeigniter.com/user_guide/general/controllers.html To remove index.php from url use .htaccess method. Online Doc: http://www.codeigniter.com/user_guide/general/urls.html – Ajay Gokhale May 10 '16 at 05:18
0

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

Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68
0

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.

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
0

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/

Janvier123
  • 11
  • 1