【问题标题】:Symfony2 Is it possible to redirect from other places(functions)?Symfony2 是否可以从其他地方(功能)重定向?
【发布时间】:2012-10-17 09:22:38
【问题描述】:

原谅我的英语。

我不想从操作端重定向,而是从其他功能重定向。

控制器

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 的环境中执行。 我怎样才能做到这一点? 或者,有没有更好的方法?

【问题讨论】:

    标签: redirect symfony controller response


    【解决方案1】:

    执行以下操作:

    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;
    }
    

    【讨论】:

    • 它完全有效。谢谢你给我的建议。 (但是,它会变得像双重检查。(从个人角度来看)如果可能的话,我想将所有检查处理都交给其他函数。)
    • 您不能在其他功能中执行所有操作。如果你从它返回,你只是返回到调用它的动作函数。请记得采纳答案
    【解决方案2】:

    如果不从editAction 返回RedirectResponse,就无法从is_review_granted 重定向。所以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

    【讨论】:

    • AccessDeniedException 很有趣。好吧,我应该阅读更多文档……感谢您的考虑。谢谢你的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2014-12-26
    • 2012-01-19
    相关资源
    最近更新 更多