Thursday 28 February 2019

Laravel exception rednder, and redirect to VUE for 404 on production

app/exceptions/handler.php

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {

        // Http not found exception //redirect to vue index.html file
        if ($this->isHttpException($exception) )
        {
            switch ($exception->getStatusCode())
            {
                // not found
                case 404:
                    return redirect()->guest('/');
                break;

            }
        }



        // In Global namespace
        // Handle JWT exeception
        if ( $exception instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException ) {
            return response()->json([ 'error' => 'Token is invalid', 'http_status' => 400]);
        }
       

        if ( $exception instanceof  \Tymon\JWTAuth\Exceptions\TokenExpiredException ) {
            return response()->json([ 'error' => 'Token is expired', 'http_status' =>  400]);
        }


        if ( $exception instanceof  \Tymon\JWTAuth\Exceptions\JWTException ) {
            return response()->json([ 'error' => 'There is problem with token', 'http_status' => 400]);
        }


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


https://stackoverflow.com/questions/29479409/redirect-to-homepage-if-route-doesnt-exist-in-laravel-5

No comments:

Post a Comment