Redirect Symfony2是否可以从其他位置(功能)重定向?
原谅我的英语 我不想从操作端重定向,而是从另一个函数重定向 控制器Redirect Symfony2是否可以从其他位置(功能)重定向?,redirect,symfony,controller,response,Redirect,Symfony,Controller,Response,原谅我的英语 我不想从操作端重定向,而是从另一个函数重定向 控制器 public function editAction(Request $request, $id) { $em = $this->getDoctrine()->getEntityManager(); $article = $em->getRepository('MySampleBundle:Article')->find($id); // TODO: // Since o
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的环境中运行。
我怎样才能做到这一点?
或者,有什么更好的方法吗?执行以下操作:
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;
}
做一些类似于:
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;
}
如果不从
editAction
返回RedirectResponse
,则无法从is\u review\u promited
重定向。所以卡洛斯·格拉纳多斯的答案是正确的
另一个选项是在is_review_grated方法中抛出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');
}
}
您还可以查看一些更深入的解决方案,如和。如果不返回
编辑操作中的重定向响应,则无法从已授予的进行重定向。所以卡洛斯·格拉纳多斯的答案是正确的
另一个选项是在is_review_grated方法中抛出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');
}
}
您还可以寻找一些更深入的解决方案,如和。它完全有效。谢谢你给我的建议。(然而,它将变得像一个双重检查。(从个人的角度来看)如果可能的话,我想把所有的检查处理都交给另一个函数。)你不能在另一个函数中做所有的事情。如果从它返回,则返回调用它的action函数。请记住接受答案,它完全起作用了。谢谢你给我的建议。(然而,它将变得像一个双重检查。(从个人的角度来看)如果可能的话,我想把所有的检查处理都交给另一个函数。)你不能在另一个函数中做所有的事情。如果从它返回,则返回调用它的action函数。请记住接受答案AccessDeniedException很有趣。嗯,我应该多看一些文档。。。。。。谢谢你的体谅。谢谢你的建议。AccessDeniedException很有趣。嗯,我应该多看一些文档。。。。。。谢谢你的体谅。谢谢你的建议。