php symfony2 并抛出异常错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10625491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
symfony2 and throwing exception error
提问by jini
I am trying to throw exceptions and I am doing the following:
我正在尝试抛出异常,并且正在执行以下操作:
use Symfony\Component\HttpKernel\Exception\HttpNotFoundException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
I am then using them the following way:
然后我按以下方式使用它们:
throw new HttpNotFoundException("Page not found");
throw $this->createNotFoundException('The product does not exist');
however I am getting errors like HttpNotFoundException is not found etc.
但是我收到了诸如 HttpNotFoundException is not found 之类的错误。
Is this the best way to throw exceptions?
这是抛出异常的最佳方式吗?
回答by Chris McKinnel
Try:
尝试:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
and
和
throw new NotFoundHttpException("Page not found");
I think you got it a bit backwards :-)
我想你有点倒退了:-)
回答by HMagdy
in any controller you can use this for 404 HTTP response inside Symfony
在任何控制器中,您都可以将其用于Symfony 中的 404 HTTP 响应
throw $this->createNotFoundException('Sorry not existing');
same as
与...一样
throw new NotFoundHttpException('Sorry not existing!');
or this for 500 HTTP response code
或此为500 HTTP 响应代码
throw $this->createException('Something went wrong');
same as
与...一样
throw new \Exception('Something went wrong!');
or
或者
//in your controller
$response = new Response();
$response->setStatusCode(500);
return $response;
or this is for any type of errors
或者这是针对任何类型的错误
throw new Symfony\Component\HttpKernel\Exception\HttpException(500, "Some description");
Also ... For Custom Exceptionyou can flow this URL
另外...对于自定义异常,您可以流此 URL
回答by Chopchop
If its in a controller, you can do it this way :
如果它在控制器中,你可以这样做:
throw $this->createNotFoundException('Unable to find entity.');
回答by Nuno Pereira
In the controller, you can simply do:
在控制器中,您可以简单地执行以下操作:
public function someAction()
{
// ...
// Tested, and the user does not have permissions
throw $this->createAccessDeniedException("You don't have access to this page!");
// or tested and didn't found the product
throw $this->createNotFoundException('The product does not exist');
// ...
}
In this situation, there'se no need to include the use Symfony\Component\HttpKernel\Exception\HttpNotFoundException;at the top. The reason for that is that you're not using the class directly, like using the constructor.
在这种情况下,不需要use Symfony\Component\HttpKernel\Exception\HttpNotFoundException;在顶部包含。原因是您没有直接使用该类,就像使用构造函数一样。
Outside the controller, you must indicate where the class can be found, and throw an exception as you would normally do. Like this:
在 controller 之外,您必须指明在哪里可以找到该类,并像往常一样抛出异常。像这样:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
// ...
// Something is missing
throw new HttpNotFoundException('The product does not exist');
or
或者
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
// ...
// Permissions were denied
throw new AccessDeniedException("You don't have access to this page!");

