bcit-ci / CodeIgniter

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
18.27k stars 7.61k forks source link

404 Page Not Found - The page you requested was not found. #1967

Closed it-can closed 11 years ago

it-can commented 11 years ago

I am getting a "404 Page Not Found The page you requested was not found." error when I go to my site: www.test.com

I have a the following routes.php

$route['default_controller'] = 'index';
$route['404_override'] = '';

/* CMS ROUTES */
$route['page/(:any)'] = 'index/page/$1';

I use also MY_Controller that extends CI_Controller, a quick view in the code I suspect that the problem is somewhere in this?

Urls with www.test.com/page/test-page seems to work. But the default page (just www.test.com) shows a 404. This worked before...

narfbg commented 11 years ago

Does your controller file happen to be named 'Index.php' instead of 'index.php'?

it-can commented 11 years ago

controller is named index.php

I use also MY_Controller that extends CI_Controller, a quick view in the code I suspect that the problem is somewhere in this?

it-can commented 11 years ago
index.php

class Index extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('includes/template', array('data'=>'123'));
    }
}

MY_Controller.php

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->vars(array('version'=>123));
    }
}
narfbg commented 11 years ago

OK, the root of the "problem" is an is_callable check in system/core/CodeIgniter.php: https://github.com/EllisLab/CodeIgniter/commit/38e32f643492a7bf0233bb9848138d183fbdfcd4#L0R298

And the reason that it returns FALSE is something that many people (you included) would consider a bug, but it's NOT: https://bugs.php.net/bug.php?id=62208&edit=1

Now, you'd probably want to revert the latest changes and decide to not use is_callable() (or to use is_callable(array(new $class, $method))), but here's why it shouldn't be done ...

$ cat test.php 
<?php
class Index {

public function __construct()
{
    echo "foo\n";
}

public function index()
{
    echo "bar\n";
}

}

new index();
$ php test.php 
foo

So far so good - it would work as expected, for you. But consider somebody that doesn't override __construct():

$ cat test.php 
<?php
class Index {

public function index()
{
    echo "bar\n";
}

}

new index();
$ php test.php 
bar

... their index() method will be considered a constructor and that's a very nasty false-positive.

The conclusion is something that is never mentioned in the docs, but is known to everybody who has ever used PHP 4 - you should never use a matching class->method pair.

it-can commented 11 years ago

okay didn't know that... I changed the index method to start() and changed the default route to index/start...

seems to work now... Thanks

kavindaps commented 9 years ago

Thanks narfbg.. I had a issue with this 404 error. your answer helped :+1: