Support the ongoing development of Laravel.io →
Requests IOC Views
Last updated 1 year ago.
0

@qICEp, I did read the docs long before posting the question. I also said that I can do the simple return Response trick suggested is the docs, did I miss something relevant to my question?

Last updated 1 year ago.
0

I think that when you use [code]App::make('FrontController')->showError($code); [/code] you already returned some view in controller and that is why you can not call showError function. (Still not sure as im just getting used with php and laravel)

In documentation they say [code]If an exception handler returns a response, that response will be sent to the browser and no other error handlers will be called:[/code] "that response will be sent to the browser" If its sent, than its sent, you can not modify that response.

You can try with nested views...something like: [code]$view = View::make('content')->nest('subview', 'frontend.errors.404'); return Response::view( $view , array(), 404); [/code]

Or you could also try to make ErrorController that extends FrontController and call showError (Not sure if its gonna work)

Last updated 1 year ago.
0

@qICEp I only return one response and the whole nested views logic is already implemented in controller methods - that's the reason I wanted to call them.

Anyway I think I figured it out, the call works as intended like that

return App::make('FrontController')->callAction('showError', array($code));

the problem was that the system didn't "notice" an error somewhere between controller and view and I just got an incomplete view. After testing the same call on another method / view I got it working.

Thanks for your support!

Last updated 1 year ago.
0

upd: to give a more polished answer - call to the controller works as described, but to return the correct response it has to be saved in a variable (it returns a view) and used to make a response instance with correct response code. So here's the global.php code:

App::error(function(Exception $exception, $code)
{
    Log::error($exception);

    if (Config::get('app.debug')) {
	    return;
    }

    switch ($code)
    {
        case 403: 
        case 404:            
        case 500: 
            $view = App::make('FrontController')->callAction('showError', array($code));
            $response = Response::make($view, $code);
            return $response;
            break;

        default:
            return Response::view('errors.default', array(), $code);
            break;
    }
});

this way errors can be handled with controller methods and google wouldn't be confused with all the 200 response codes

Last updated 1 year ago.
0

I think the correct method for handling errors is to catch/throw a specific exception type with whatever message/code you choose. If you do this, then in App::error you are able to respond to specific types of exceptions/codes and format the response to your choosing. You can also group these cases neatly into a single folder to keep things modular. I am by no means an expert as I have just started using laravel, but this seemed to make the most sense to me.

For example: I throw an OAuth2Exception while checking OAuth2 params for a specific route. I want a specific json output to be returned as well as a specific http status code (400).

I created the folder app/exceptions and added app/exceptions to the composer.json

composer.json

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/exceptions",
        "app/filters",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
},

Inside app/exceptions I added a file name Oauth2Exception app/exceptions/Oauth2Exception.php

App::error(function (OAuth2Exception $e, $code, $fromConsole) {
    Log::error('OAuth2Exception - ' . $e->getMessage());
    return Response::json(["error" => $e->getMessage()], 400);
});

class OAuth2Exception extends Exception {
}

I created this custom Oauth2Exception and then type hinted the exception to catch in App::error() as specified in the docs. This allows you to have an implementation for each exception you create. In this specific situation, I can throw a 400 http status code with json response :)

Hope this helps!

Last updated 1 year ago.
0

Had a lot of issues with this, I kept getting an error on method filter() on NULL.

So I switched my missing method up to the BaseController.

In global.php

App::error(function($exception, $code)
{
    Log::error($exception);
    return App::make('BaseController')->callAction('missing', array($code));

});

In BaseController.php

/**
     * @param $code
     * @return \Illuminate\View\View
     */
    public function missing($code)
    {
        return View::make('errors.'.$code, array($code));
    }
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

yutto yutto Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.