使用函数中的新参数覆盖控制器 Synfony2
Overriding controller Synfony2 with a new parameter in a function
我正在覆盖 FOSUserBundle 中的 SecurityController。
我尝试在登录后保留一些数据,但为此我需要将 "Request $request" 参数添加到函数 protected function renderLogin 但我收到以下错误:
运行时注意:Utilisateurs\UtilisateursBundle\Controller\SecurityController::renderLogin() 的声明应该与 FOS\UserBundle\Controller\SecurityController::renderLogin(array $data)
兼容
这是我的代码:
/**
* Renders the login template with the given parameters. Overwrite this function in
* an extended controller to provide additional data for the login template.
*
* @param array $data
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderLogin(array $data, Request $request)
{
function is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
if (is_session_started() == TRUE)
{
$session = $request->getSession();
$content = $session->get('result');
$idQuery = $session->get('query_id');
$authorId = $this->getUser()->getId();
$biblio = new Biblio;
$biblio->setAuthorId($authorId);
$biblio->setContent($content);
$biblio->setQueryId($idQuery);
$biblio->setDate(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($biblio);
$em->flush();
$session->clear();
$response = $this->forward('BiblishareBundle:Profile:show');
return $response;
}
else
{
return $this->render('FOSUserBundle:Security:login.html.twig', $data);
}
}
谢谢你的帮助
当您使用不同的参数集覆盖方法时,它会产生 E_STRICT 错误 - 这是 php 特定的,Symfony 只是遵循标准。所以看来你需要以不同的方式解决你的问题。如何使用:
$request = $this->getRequest()
在你的方法中(我假设你的控制器的基础 class 是 Symfony 的 Controller
)
检查这些以获取更多信息:
PHP: "Declaration of ... should be compatible with that of ..."
adding parameters to overridden method E_STRICT observation
Why is overriding method parameters a violation of strict standards in PHP?
注意:您那里的代码很乱。您可以使用 $this->get('session')->isStarted()
检查会话是否已启动
我建议您改为监听登录事件。
这里是这样描述的:
并且可以通过使用 fos_user.security.interactive_login
事件应用于 FOSUserBundle。
这样您就不必从 FOSUserBundle 覆盖 SecurityController,但您可以在登录后保留数据。
我正在覆盖 FOSUserBundle 中的 SecurityController。 我尝试在登录后保留一些数据,但为此我需要将 "Request $request" 参数添加到函数 protected function renderLogin 但我收到以下错误:
运行时注意:Utilisateurs\UtilisateursBundle\Controller\SecurityController::renderLogin() 的声明应该与 FOS\UserBundle\Controller\SecurityController::renderLogin(array $data)
兼容这是我的代码:
/**
* Renders the login template with the given parameters. Overwrite this function in
* an extended controller to provide additional data for the login template.
*
* @param array $data
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderLogin(array $data, Request $request)
{
function is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
if (is_session_started() == TRUE)
{
$session = $request->getSession();
$content = $session->get('result');
$idQuery = $session->get('query_id');
$authorId = $this->getUser()->getId();
$biblio = new Biblio;
$biblio->setAuthorId($authorId);
$biblio->setContent($content);
$biblio->setQueryId($idQuery);
$biblio->setDate(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($biblio);
$em->flush();
$session->clear();
$response = $this->forward('BiblishareBundle:Profile:show');
return $response;
}
else
{
return $this->render('FOSUserBundle:Security:login.html.twig', $data);
}
}
谢谢你的帮助
当您使用不同的参数集覆盖方法时,它会产生 E_STRICT 错误 - 这是 php 特定的,Symfony 只是遵循标准。所以看来你需要以不同的方式解决你的问题。如何使用:
$request = $this->getRequest()
在你的方法中(我假设你的控制器的基础 class 是 Symfony 的 Controller
)
检查这些以获取更多信息:
PHP: "Declaration of ... should be compatible with that of ..."
adding parameters to overridden method E_STRICT observation
Why is overriding method parameters a violation of strict standards in PHP?
注意:您那里的代码很乱。您可以使用 $this->get('session')->isStarted()
我建议您改为监听登录事件。
这里是这样描述的:
并且可以通过使用 fos_user.security.interactive_login
事件应用于 FOSUserBundle。
这样您就不必从 FOSUserBundle 覆盖 SecurityController,但您可以在登录后保留数据。