fuel / core

Fuel PHP Framework - The core of the Fuel v1 framework
http://fuelphp.com
813 stars 345 forks source link

Better way of handling 404 && 500 errors #1615

Closed alrik11es closed 10 years ago

alrik11es commented 10 years ago

Until now I've always had to edit HttpNotFoundException and HttpServerErrorException directly in the core without the possibility of subtract this to my app to use my own view.

The thing is that this is a module agnostic framework at this point because if I have to update the core I will overwrite this thing causing me troubles on a good framework-app separation.

WanWizard commented 10 years ago

Eh, why is that?

The default 404.php and 500.php view files can be overridden in app, just like anything else from the core? And if you want more, you can extend both Exception classes in app too. Although the preferred why of dealing with this is to catch the exception in your index.php, like it is done by default.

There is never a need to modify framework core code.

alrik11es commented 10 years ago

Could you be more specific? I've tried to extend the classes from the app but didn't work. I think that the problem could be that the docs are not very clear then.

I've tried something like this in the classes root inside my app. I guess that is correct but I don't know how to override the framework to use this file instead of the default one.

namespace Fuel\Core;

class HttpNotFoundOwnException extends HttpNotFoundException
{
    public function response()
    {
        $this->data = array();
        $this->data['path'] = Uri::base(false);
        return new \Response(\View::forge('errors/404.twig', $this->data), 404);
    }
}
kenjis commented 10 years ago

See http://fuelphp.com/docs/general/extending_core.html#/extend_and_replace

How about this?

fuel/app/classes/httpnotfoundexception.php:

class HttpNotFoundException extends Fuel\Core\HttpNotFoundException
{
    public function response()
    {
        $this->data = array();
        $this->data['path'] = Uri::base(false);
        return new \Response(\View::forge('errors/404.twig', $this->data), 404);
    }
}

fuel/app/bootstrap.php:

Autoloader::add_classes(array(
    // Add classes you want to override here
    // Example: 'View' => APPPATH.'classes/view.php',
    'HttpNotFoundException' => APPPATH.'classes/httpnotfoundexception.php',
));
alrik11es commented 10 years ago

Thanks. Really helpful. BTW this framework is incredible keep it up!