jin*_*ini 21 php exception symfony
我试图抛出异常,我正在做以下事情:
use Symfony\Component\HttpKernel\Exception\HttpNotFoundException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
Run Code Online (Sandbox Code Playgroud)
然后我按照以下方式使用它们:
throw new HttpNotFoundException("Page not found");
throw $this->createNotFoundException('The product does not exist');
Run Code Online (Sandbox Code Playgroud)
但是我收到的错误就像找不到HttpNotFoundException等.
这是抛出异常的最佳方式吗?
Chr*_*nel 49
尝试:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Run Code Online (Sandbox Code Playgroud)
和
throw new NotFoundHttpException("Page not found");
Run Code Online (Sandbox Code Playgroud)
我觉得你有点倒退了:-)
HMa*_*gdy 30
在任何控制器中,您都可以在Symfony中使用它来进行404 HTTP响应
throw $this->createNotFoundException('Sorry not existing');
Run Code Online (Sandbox Code Playgroud)
与...一样
throw new NotFoundHttpException('Sorry not existing!');
Run Code Online (Sandbox Code Playgroud)
或者这个500 HTTP响应代码
throw $this->createException('Something went wrong');
Run Code Online (Sandbox Code Playgroud)
与...一样
throw new \Exception('Something went wrong!');
Run Code Online (Sandbox Code Playgroud)
要么
//in your controller
$response = new Response();
$response->setStatusCode(500);
return $response;
Run Code Online (Sandbox Code Playgroud)
或者这是针对任何类型的错误
throw new Symfony\Component\HttpKernel\Exception\HttpException(500, "Some description");
Run Code Online (Sandbox Code Playgroud)
另外...对于自定义例外, 您可以传递此URL
如果它在控制器中,你可以这样做:
throw $this->createNotFoundException('Unable to find entity.');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75202 次 |
| 最近记录: |