Php Symfony 2从专用功能重定向
在我的应用程序中,我使用init函数初始化一个动作 init函数验证用户输入 (例如,用户正在查找不存在的产品->初始化函数应将其重定向到错误页面“产品…未找到”) 这个控制器中有一个私有函数,它应该验证给定参数的url,如果它是错误的(数据库中不存在),私有函数应该重定向Php Symfony 2从专用功能重定向,php,symfony,Php,Symfony,在我的应用程序中,我使用init函数初始化一个动作 init函数验证用户输入 (例如,用户正在查找不存在的产品->初始化函数应将其重定向到错误页面“产品…未找到”) 这个控制器中有一个私有函数,它应该验证给定参数的url,如果它是错误的(数据库中不存在),私有函数应该重定向 private function init($var) { if($this->databasesearchforexyz($var)){ // redirect to Errorpage (N
private function init($var)
{
if($this->databasesearchforexyz($var)){
// redirect to Errorpage (No xyz found named ...)
return $this->redirect($this->generateUrl('xyz_error_...'));
}
if($this->checksomethingelse($var)){
// redirect to some other error page
}
}
请注意,这些不是我真正的方法/变量/路径等名称
问题是,它不是重定向。这里有一些建议 如果没有重定向,则从init函数返回
true
,如果有重定向,则返回false
例如:
private function init($var) {
if ($error) {
// An error occurred, redirect
$this->redirect($this->generateUrl('xyz_error_...'));
return false;
}
// Else, everything alright
return true;
}
public function indexAction ($var) {
if (!$this->init($var)) {
// Failed to init, redirection happening
return;
}
// Continue as normal
}
您可以检查init函数是否返回实际响应,然后直接从主代码返回。像这样:
public function indexAction ($var)
{
$xyz = $this->initxyz($var);
if ($xyz instanceof \Symfony\Component\HttpFoundation\Response) {
return $xyz;
}
...
.. more code
.
}
顺便说一句,如果您只需要检查数据库是否存在,您可以使用symfony的使用@alex88的答案,我聚合了一个异常和一个异常侦听器来执行重定向。这避免了我反复重复这个条件,因为我的函数可以在不同的场景下重定向用户 1。控制器
namespace AppBundle\Controller;
use AppBundle\Exception\UserHasToBeRedirectedException;
class DefaultController extends Controller
{
public function indexAction(...)
{
...
$this->userHasToBeRedirected();
...
}
private function userHasToBeRedirected()
{
...
if ($userHasToBeRedirected) {
$response = $this->redirect($this->generateUrl(...));
throw new UserHasToBeRedirectedException($response);
}
...
}
}
namespace AppBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use AppBundle\Exception\UserHasToBeRedirectedException;
class ExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
...
if ($exception instanceof UserHasToBeRedirectedException) {
$response = $exception->getResponse();
$event->setResponse($response);
}
...
}
}
2。例外情况
namespace AppBundle\Exception;
use Exception;
use Symfony\Component\HttpFoundation\Response;
class UserHasToBeRedirectedException extends Exception
{
private $response;
public function __construct(Response $response)
{
$this->response = $response;
}
public function getResponse()
{
return $this->response;
}
public function setResponse(Response $response)
{
$this->response = $response;
return $this;
}
}
3。异常侦听器
namespace AppBundle\Controller;
use AppBundle\Exception\UserHasToBeRedirectedException;
class DefaultController extends Controller
{
public function indexAction(...)
{
...
$this->userHasToBeRedirected();
...
}
private function userHasToBeRedirected()
{
...
if ($userHasToBeRedirected) {
$response = $this->redirect($this->generateUrl(...));
throw new UserHasToBeRedirectedException($response);
}
...
}
}
namespace AppBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use AppBundle\Exception\UserHasToBeRedirectedException;
class ExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
...
if ($exception instanceof UserHasToBeRedirectedException) {
$response = $exception->getResponse();
$event->setResponse($response);
}
...
}
}
4。在service.yml注册服务
...
appBundle.exception_listener:
class: AppBundle\EventListener\ExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception }
...
有关更多信息:
这不起作用,它不会重定向这会起作用,但在我看来,alex88的解决方案对我的应用程序更有用,谢谢你的努力。我也考虑过这一点,但它又产生了一个不好的if语句。若并没有私有方法,那个么就不需要If语句——只要立即返回即可。所以我们需要更好的解决方案。使用异常听起来也是错误的——因为异常是在出现错误时出现的。但如果有成功,听起来并不好。