如果网址具有不同的参数或路由不存在,如何重定向页面?在拉拉韦尔

时间:2018-07-25 07:00:49

标签: laravel laravel-5.6

如果路由不存在,如何重定向页面

'sitename.me/asdasdas'

因为当我尝试这样做时 NotFoundHttpException将显示。

请帮助我谢谢

3 个答案:

答案 0 :(得分:1)

您可以为此更改app/Exceptions/Handler.php。将render()unauthenticated()函数替换为以下内容。

public function render($request, Exception $exception) {
    if ($this->isHttpException($exception)) {
        switch ($exception->getStatusCode()) {
            // not found
            case 404:
                return redirect()->guest(your redirect url));
                break;

            // internal error
            case '500':
                return redirect()->guest(your redirect url));
                break;

            default:
                return $this->renderHttpException($exception);
                break;
        }
    } else {
        return parent::render($request, $exception);
    }
}

protected function unauthenticated($request, AuthenticationException $exception) {
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest(your redirect url);
}

答案 1 :(得分:0)

要重定向到外部域,请使用以下命令:

return redirect()->away('http://sitename.me/asdasdas');

要重定向到内部URI,请使用:

return redirect('admin/dashboard');

要重定向到应用程序路由,请使用:

return redirect()->route('admin.dashboard');

要进一步参考,请查看responses (redirects) documentation

答案 2 :(得分:0)

让我解释一下我所遇到的一些细节,Laravel在app / Exceptions文件夹中抛出常见异常,并抛出Handler.php文件。在此文件中,我们具有使用渲染功能引发常见异常的报告功能。

public function report(Exception $exception)
{
    if ($exception instanceof \League\OAuth2\Server\Exception\OAuthServerException){
        $this->render(null,$exception);
    } else {
        parent::report($exception);
    }
}

上面是一个报告函数,会引发任何异常以渲染下面指定的函数

public function render($request, Exception $exception)
{        
    if($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException){
       // return Helper::responseJson(null,'header','Page Not Found',404);
    } else if ($exception instanceof ScopeHandler) {
        return Helper::responseJson(null,'header','Unauthorized',401);
    } else if ($exception instanceof \League\OAuth2\Server\Exception\OAuthServerException){
        return Helper::responseJson(null,'token','Unauthorized',401);
    } else if ($exception instanceof ModelNotFoundException) {
       // return Helper::responseJson(null,'header','Model Not Found',404);            
    } else if ($exception instanceof \Illuminate\Database\QueryException){
       // return Helper::responseJson(null,'header','Query Exception',400);            
    } else if ($exception instanceof \Swift_TransportException){
        return Helper::responseJson(null,'header','Mail send Exception',400);            
    }else if ($exception instanceof ConnectionFailedException){
        return Helper::responseJson(null,'header','Connection Exception',400);            
    }
     /* else if($exception instanceof \Symfony\Component\Debug\Exception\FatalErrorException){
        return Helper::responseJson(null,'header','PHP config Exception',400);             
    } */

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

上面的渲染函数示例,我们使用json格式用于抛出异常。我们可以重定向到特定页面,或者在抛出这些错误时可以执行任何功能。