CodeIgniter Forums
The 404 problems (CI Reactor) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: The 404 problems (CI Reactor) (/showthread.php?tid=38753)

Pages: 1 2


The 404 problems (CI Reactor) - El Forum - 02-17-2011

[eluser]victorche[/eluser]
I know that there are a lots of topics about it and if the moderators think that this post is not useful, they can delete it. I want to describe the problems here in details, like a summary.
Now we have a new config value in /application/config/routes.php:
Code:
$route['404_override'] = 'error';
So I am making a new file /application/controllers/error.php, which is simple enough:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Error extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $data = array();

        $this->template->set('title', $this->lang->line('title_error'));
        $this->template->load('template', 'error', $data);
    }
}

/* End of file error.php */
/* Location: ./application/controllers/error.php */
So, there is a simple view for this too (will not post the code, as it is not important in this case).
And what's happening... If i call something like:
http://example.com/missing_controller
The result is as expected, my custom 404 error page is loaded.
But let's show you a simple file, like this:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Posts extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        // Some code here ...
    }

    function view($id)
    {
                $post = $this->posts->get_post($id);

                if ($post)
                {
                $data = array();

                $this->template->set('title', $this->lang->line('title_posts'));
                $this->template->load('template', 'posts', $data);
                }
                else
                {
                        show_404();
                }
    }
}

/* End of file posts.php */
/* Location: ./application/controllers/posts.php */
And with this file, if i go to:
http://example.com/posts/view/missing_id
it gives me the default 404 error page. i think this is not the way it should be? This is problem number one... and if i go to:
http://example.com/posts/missing_method
it gives me
Quote:Message: Undefined property: Missing::$template
Which means... the autoloaded libraries (template in this case) are not loaded anymore. Which is problem number two...
Can some of the developers give me a hint here? I am totally stuck with these two problems. Testing this with the current branch of CI reactor, downloaded today.


The 404 problems (CI Reactor) - El Forum - 02-17-2011

[eluser]InsiteFX[/eluser]
Well I know for a fact that if the core calls autoload and then it is called again it will overwrite the first autoload unless it was saved and added to the second autoload. This is way they came out with the SPL_Autoload method.

InsiteFX


The 404 problems (CI Reactor) - El Forum - 02-18-2011

[eluser]victorche[/eluser]
so, @InsiteFX... You want to say that the described behaviour is normal?


The 404 problems (CI Reactor) - El Forum - 02-18-2011

[eluser]InsiteFX[/eluser]
Read this article and take note at the bottom of it about overwriting another autoload.

SPL_Autoload

InsiteFX


The 404 problems (CI Reactor) - El Forum - 02-18-2011

[eluser]victorche[/eluser]
I see but ... I am not so good programmer. All I need is an answer, is this the correct way? If it is, then it means that I am making something wrong


The 404 problems (CI Reactor) - El Forum - 02-18-2011

[eluser]Phil Sturgeon[/eluser]
This is a valid point victorche that has nothing to do with SPL_Autoload.

The 404_override solution is a Routing solution. If when you call a URL it cannot find anything, it will route to the controller you specified. However once you call show_404() this will not try to route again as you are already in a controller.

This is not so much a bug, just an unexpected usage of the feature. To get this working would involve implementing a solution very similar to HMVC (not talking about Modular Extensions or any module functionality, just the ability to call another controller from a controller). This is a known topic, but for now I would just suggest you install HMVC and make a call to that controller, or redirect with a HTTP 1.1 Status 404 header set to let Google/Browser know the page was a 404.


The 404 problems (CI Reactor) - El Forum - 02-18-2011

[eluser]victorche[/eluser]
Thanks, @Phil! I got it! But anyway ... Your answer covers only one of my problems. I still don't understand why I have the problem with the autoloaded libraries, when trying to call a missing method. Not 404 page (default or custom one), but php warning.

The autoload problem is described here in details:
http://ellislab.com/forums/viewthread/178989/P15/

@phil, please share your thoughts...


The 404 problems (CI Reactor) - El Forum - 02-24-2011

[eluser]bubbafoley[/eluser]
I have a working solution. I modified show_error() in system/core/Exceptions.php to check for overrides and run that controller if found. I'm still trying to figure out the best way to call the error controller but it works.

https://bitbucket.org/bubbafoley/codeigniter-404s/changeset/8b4e6864af41


The 404 problems (CI Reactor) - El Forum - 02-24-2011

[eluser]Glazz[/eluser]
[quote author="bubbafoley" date="1298586499"]I have a working solution. I modified show_error() in system/core/Exceptions.php to check for overrides and run that controller if found. I'm still trying to figure out the best way to call the error controller but it works.

https://bitbucket.org/bubbafoley/codeigniter-404s/changeset/8b4e6864af41[/quote]

That is working for me, doesn't show any errors for now.


The 404 problems (CI Reactor) - El Forum - 02-24-2011

[eluser]InsiteFX[/eluser]
Phil,

Quote:This is a valid point victorche that has nothing to do with SPL_Autoload.

I know this has nothing to do with the problem, I was giving him an example of what happens when an __autoload is set and some other library sets its own __autoload it over writes the first one if not saved.

Example: Like your config __autoload.
Now if I load a library that has its own __autoload method what do think is going to happen to yours?

The SPL_Autoload was just and example to show how it fixes it.

And HMVC does you SPL_Autoload.

InsiteFX