JackD's avatar

@rodrigo.pedra should i edit the RouteServiceProvider.php to redirect when they try to access a non existing username? or is it other file i should edit?

rodrigo.pedra's avatar

I handle it on my app/Exceptions/Handler class render() method like this:

public function render( $request, Exception $e )
{
    if ( $e instanceof ModelNotFoundException )
    {
        return redirect()->back()->withInput()->withErrors( 'The resource does not exist' );
    }

    return parent::render( $request, $e );
}

I actually check for many more exceptions, it would very long to paste here, but this exception is the one raised by ->firstOrFail() when a model is not found.

I prefer to handle it here because of the separation of concerns principle, which says one class should be responsible for one task only. So handling exception belongs to this class.

see the laracasts collections about SOLID principles: [ https://laracasts.com/collections/solid-principles ].

JackD's avatar

thanks bro will watch that laracasts video, again thanks a lot what a day hahaha

2 likes
Previous

Please or to participate in this conversation.