Codeigniter controller not found

≡放荡痞女 提交于 2020-02-02 03:03:32

问题


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


回答1:


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



回答2:


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]



回答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




回答4:


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.




回答5:


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.




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/37114756/codeigniter-controller-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!