Symfony2是否可以从其他地方(功能)重定向?

时间:2012-10-17 09:22:38

标签: redirect symfony controller response

原谅我的英语。

我想不是从动作方面重定向,而是从其他功能重定向。

控制器

public function editAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $article = $em->getRepository('MySampleBundle:Article')->find($id);

    // TODO:
    // Since other actions have the same processing,
    // I would like to do check work in other function. 
    // And when inaccurate,
    // I want to make it move from that function to other page. 
    $this->is_edit_granted($article);

    $form = $this->createForm(new ArticleType(), $article);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        if ($form->isValid()) {
            // ...
        }
    }

    return $this->render('MySampleBundle:Article:edit.html.twig', array(
        'form' => $form->createView(),
        'article' => $article,
    ));
}

public function is_edit_granted($article)
{
    // TODO:
    // I check not at the action side but at this place,
    // and want to make it move from this function to other page. 

    if (!$article) {
        throw $this->createNotFoundException('No article found.');
    } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
        return $this->redirect( // doesn't work here
            $this->generateUrl('home', array(
                "id" => $article->getId()
            ))
        );
    }
}

我也尝试过类似的代码:

use Symfony\Component\HttpFoundation\RedirectResponse;

class SampleController extends Controller
{
    // ...

    public function editAction(Request $request, $id)
    {
        // ...

        $this->is_edit_granted($article);

        // ...
    }

    public function is_edit_granted($article)
    {
        if (!$article) {
            throw $this->createNotFoundException('No article found.');
        } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
            return new RedirectResponse(
                $this->generateUrl('home', array(
                    "id" => $article->getId()
                ))
            );
        }
    }
}

但它不起作用。

它在Symfony 2.1.2的环境中运行。 我怎样才能实现这一目标? 或者,有没有更好的方法?

2 个答案:

答案 0 :(得分:3)

做类似的事情:

public function editAction(Request $request, $id)
{
    // ...

    $response = $this->is_edit_granted($article);
    if ($response) return $response;

    // ...
}

public function is_review_granted($article)
{
    if (!$article) {
        throw $this->createNotFoundException('No article found.');
    } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
        return new RedirectResponse(
            $this->generateUrl('home', array(
                "id" => $article->getId()
            ))
        );
    }
    return null;
}

答案 1 :(得分:1)

如果不is_review_granted表单RedirectResponse,则无法从editAction重定向。所以Carlos Granados的答案是正确的。

另一种选择是在is_review_granted方法中抛出AccessDeniedException:

public function is_review_granted($article)
{
    if (!$article) {
        throw $this->createNotFoundException('No article found.');
    } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
        throw new Symfony\Component\Security\Core\Exception\AccessDeniedException('no acces');
    }
}

您还可以查看一些更深入的解决方案,例如ACLSecurityVoters