Codeigniter 3.0.0 - error 404 page not found
Asked Answered
Q

4

5

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

.htaccess inside application folder

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

autoload.php

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

config.php

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

routes.php

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

post.php in model folder

    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

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

Quamash answered 17/7, 2015 at 14:18 Comment(4)
if your using Codeigniter 3.0 then post.php controller should be Post.php Don't touch htaccess in applications folder only add one in main directory.Redingote
Also since you have not removed index.php in config ad set htaccess in main directory then your url will need to have index.php example.com/index.php/controllerRedingote
Hi, not sure what you mean, should it be like this $this->load->model('Post');?Quamash
I would re-name post model to Model_post.php because some times codeigniter gets confused the controller and models must have there first letter on filename and class name uppercase and then you can use $this->load->model('model_post') and $this->model_post->function()Redingote
R
16

When using 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

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

Filename: 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

Filename: 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.

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

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.

Redingote answered 18/7, 2015 at 1:17 Comment(5)
first letter of class name and file name as upper case does not create the problem i am currently working in itBabi
but i want to ask what will happen if we edit .htacess file in application folderBabi
@AnmolRaghuvanshi Are you using codeigniter 2 or 3 When you want to remove index.php you need a htaccess in main directory to suit your serverRedingote
i am using CI 3 i have edited .htacess file in application folder so what type of problems can ocuur....Babi
@AnmolRaghuvanshi The application folder htaccess just makes that directory secure so would not muck around it. To remove index.php you need one to suite your server on your main directory of project.Redingote
P
1

Add a route like...

 $route['posts'] = 'posts/index';
Pinnace answered 17/7, 2015 at 14:22 Comment(2)
Edited. The issue is related to the route. If you dont have a method called index then choose appropriate from your available methods. I am trying to remember how I solved these historically. Are you passing any query params?Pinnace
Still not working, I edited my question and added the functions I created.Quamash
M
1

There is one more possibility, other than the incorrect Route configuration.

I noticed a similar issue when your controller name looks like this: "ControllerName".

To resolve this you just have to rename your Controller like this: "Controllername"

Mucky answered 2/4, 2023 at 7:44 Comment(0)
F
0

In my case this solved the question:

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

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

Fantastic answered 19/3, 2017 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.