Codeigniter 3.0.0 - 404页面未找到

时间:2022-10-09 14:58:37

This is my first php framework. I have a php file in my controller which is posts.php but when I tried to run it localhost/codeigniter/index.php/posts, it displays error 404

这是我的第一个php框架。我的控制器中有一个php文件,是post。但是当我试着运行它localhost/codeigniter/index时。php/post显示错误404

.htaccess inside application folder

. htaccess内部应用程序文件夹

<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>

autoload.php

autoload.php

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

config.php

config。

$config['base_url'] = 'http://localhost/codeigniter/';
$config['index_page'] = 'index.php';

routes.php

routes.php

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

post.php in model folder

职位。php模式文件夹中

    class Post extends CI_Model{

    function get_posts($num = 20, $start = 0){

        //$sql = "SELECT * FROM users WHERE active=1 ORDER BY date_added DESC LIMIT 0,20;";
        $this->db->select()->from('posts')->where('active', 1)->order_by('date_added', 'desc')->limit(0, 20);
        $query=$this->db->get();
        return $query->result_array();

    }

}

posts.php in controller folder

职位。php控制器文件夹中

class Posts extends CI_Controller{

    function index(){

        $this->load->model('post');
        $data['posts'] = $this->post->get_posts();
        echo "<pre>";
            print_r($data['posts']);
        echo "</pre>";      

    }

}

It should display an empty array but it shows error 404 instead

它应该显示一个空数组,但它显示的是error 404

3 个解决方案

#1


15  

When using codeigniter 3

当使用codeigniter 3

All controllers and models should have there first letter of class name and file name as upper case example Welcome.php and not welcome.php

所有的控制器和模型都应该有第一个类名和文件名的字母作为大写的例子。php和不welcome.php

For your model because it is the same name as controller. I would change model name to Model_post

因为它和控制器的名称相同。我将把模型名改为Model_post

Filename: Model_post.php

文件名:Model_post.php

<?php

class Model_post extends CI_Model {

    public function some_function() {

    }

}

That way codeigniter will not get confused.

这样代码点火器就不会混淆。

Post Controller would be

Post控制器会

Filename: Post.php

文件名:Post.php

<?php

class Post extends CI_Controller {

    public function __construct() {
       parent::__construct();
       $this->load->model('model_post');
    }

   public function index() {
      $this->model_post->some_function();
   }

}

Also in your url if not set up codeigniter / htaccess to remove index.php then your url will need to use index.php every where.

如果没有设置codeigniter / htaccess以删除索引,也可以在url中使用。那么你的url将需要使用索引。php的每一个地方。

http://localhost/project/index.php/post

http://www.example.com/index.php/post

Note: do not touch the htaccess in the application folder if you need htaccess add one in main directory Htaccess For Codeigniter

注意:如果需要在Codeigniter的主目录htaccess中添加htaccess,请不要触摸应用程序文件夹中的htaccess

Previous versions of codeigniter before v3 you did not need to worry about ucfirst for controllers but now you do for version 3 and above.

在v3之前的codeigniter的以前版本中,您不需要为控制器担心ucfirst,但是现在您需要为version 3和更高版本担心。

#2


1  

Add a route like...

添加这样的路线…

 $route['posts'] = 'posts/index';

#3


0  

In my case this solved the question:

就我而言,这解决了问题:

Go to config/routes.php and define the default controller on line 52;

去配置/路线。在第52行定义默认控制器;

In case you need another function instead of index, i would call that function from index function. But that's on you.

如果你需要另一个函数而不是索引,我会从索引函数中调用这个函数。但这是你。

#1


15  

When using codeigniter 3

当使用codeigniter 3

All controllers and models should have there first letter of class name and file name as upper case example Welcome.php and not welcome.php

所有的控制器和模型都应该有第一个类名和文件名的字母作为大写的例子。php和不welcome.php

For your model because it is the same name as controller. I would change model name to Model_post

因为它和控制器的名称相同。我将把模型名改为Model_post

Filename: Model_post.php

文件名:Model_post.php

<?php

class Model_post extends CI_Model {

    public function some_function() {

    }

}

That way codeigniter will not get confused.

这样代码点火器就不会混淆。

Post Controller would be

Post控制器会

Filename: Post.php

文件名:Post.php

<?php

class Post extends CI_Controller {

    public function __construct() {
       parent::__construct();
       $this->load->model('model_post');
    }

   public function index() {
      $this->model_post->some_function();
   }

}

Also in your url if not set up codeigniter / htaccess to remove index.php then your url will need to use index.php every where.

如果没有设置codeigniter / htaccess以删除索引,也可以在url中使用。那么你的url将需要使用索引。php的每一个地方。

http://localhost/project/index.php/post

http://www.example.com/index.php/post

Note: do not touch the htaccess in the application folder if you need htaccess add one in main directory Htaccess For Codeigniter

注意:如果需要在Codeigniter的主目录htaccess中添加htaccess,请不要触摸应用程序文件夹中的htaccess

Previous versions of codeigniter before v3 you did not need to worry about ucfirst for controllers but now you do for version 3 and above.

在v3之前的codeigniter的以前版本中,您不需要为控制器担心ucfirst,但是现在您需要为version 3和更高版本担心。

#2


1  

Add a route like...

添加这样的路线…

 $route['posts'] = 'posts/index';

#3


0  

In my case this solved the question:

就我而言,这解决了问题:

Go to config/routes.php and define the default controller on line 52;

去配置/路线。在第52行定义默认控制器;

In case you need another function instead of index, i would call that function from index function. But that's on you.

如果你需要另一个函数而不是索引,我会从索引函数中调用这个函数。但这是你。