Routing in Silex/Symfony. Providing a default route

前端 未结 2 1687
傲寒
傲寒 2021-02-04 14:18

I\'m attempting to do something using Silex (which uses the Symfony routing component - so the answer may be applicable to Symfony as well)

I am adding Silex to a legacy

2条回答
  •  臣服心动
    2021-02-04 14:59

    You can use the error handling, with something like that :

    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpKernel\Exception\HttpException;
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    
    $app->error(function (\Exception $e) use ($app) {
    if ($e instanceof NotFoundHttpException) {
            return new Response('The requested page could not be found. '.$app['request']->getRequestUri(), 404);
        }
        $code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
        return new Response('We are sorry, but something went terribly wrong.', $code);
    });
    

提交回复
热议问题