2016-05-29 119 views
0

后index.html.twig成功注册后,我将呈现给index.html.twig渲染成功注册FOSUserbundle

于是在课上这个RegistrationController在功能registerAction我不得不默认:

return $this->render('FOSUserBundle:Registration:register.html.twig', array(
     'form' => $form->createView(), 
    )); 

所以我修改成:

return $this->render('AcmeCovoiturageBundle:Index:index.html.twig'), array(
     'form' => $form->createView(), 
    )); 

但因为我已经从数据库中index.html.twig信息造成的错误。

,所以我重新改变,为:

return $this->render(controller("AcmeCovoiturageBundle:Index:index"), array(
     'form' => $form->createView(), 
    )); 

,但我有这个错误:试图从命名空间中调用函数“控制器”“FOS \ UserBundle \控制器”

请审视一下,告诉我怎样我可以解决这个错误

回答

0

它取决于你所指的“index.htl.twig”文件的位置。我怀疑这是在 “默认/ index.html.twig” 这样试试这个:

return $this->render('default/index.html.twig'), array(
     'form' => $form->createView(), 
    )); 

实际路径是:应用程序/资源/视图/默认/ index.html.twig

+0

无法找到模板“index/index.html.twig。我尝试使用应用程序/ Ressources/views/default/index.html.wing,我得到无法找到模板”default/index.html.twig – Hamdy

+0

实际上现在我想到它,也许尝试'render('index.html.twig')'。让我知道,我会修改我的帖子。另一件事是你需要该文件夹下的树枝模板。例如,如果目录“app/Resources/views/default”不存在,则需要创建它。 –

+0

我找到了解决方案,我在这里发布答案。 – Hamdy

0

Finaly,我发现一个办法。 首先,我尝试渲染到registrationController的另一个页面。如果我没有在这个页面显示数据,这并不是完全错误的。 FOSUserBundle对此渲染问题的回复之一,他说,我说:“登录不是由FOSUserBundle处理,而是由Symfony安全组件处理,这个包仅用于提供用户管理” 所以我寻找另一种逻辑。 我创建了一个侦听器。我把它命名为在config.yml(项目名称/ SourcesFiles /应用/配置/ config.yml)

services: 
    fos_user.doctrine_registry: 
     alias: doctrine 
    sdz_user.registration_complet: 
     class: Acme\CovoiturageBundle\Services\RegistrationConfirmListener 
     arguments: [@router] 
     tags: 
      - { name: kernel.event_subscriber } 

所以收听者将监听来自symfony的核心事件订阅。 之后,在你的包添加文件夹,并添加一个PHP类名为RegistrationConfirmListener:

class RegistrationConfirmListener implements EventSubscriberInterface { 

private $router; 

public function __construct(UrlGeneratorInterface $router) { 
    $this->router = $router; 
} 

/** 
* {@inheritDoc} 
*/ 
public static function getSubscribedEvents() { 
    return array(
     FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess' 
    ); 
} 

public function onRegistrationSuccess(\FOS\UserBundle\Event\FormEvent $event) { 
    $url = $this->router->generate('index'); 
    $event->setResponse(new RedirectResponse($url)); 
} 

}

,您将需要从FOSUserBundle用途:

use FOS\UserBundle\FOSUserEvents 
use FOS\UserBundle\Event\UserEvent 
use Symfony\Component\EventDispatcher\EventSubscriberInterface 
use Symfony\Component\HttpFoundation\RedirectResponse 
use Symfony\Component\Routing\Generator\UrlGeneratorInterface 

要小心,如果你不要把名字空间放在顶部,symfony不会识别你的类。

namespace Acme\CovoiturageBundle\Services 

您是否看到“REGISTRATION_SUCCESS”?我借此从这个RegistrationController从功能registraionAction:

$dispatcher->dispatch(FOSUserEvents::**REGISTRATION_COMPLETED**, new FilterUserResponseEvent($user, $request, $response)); 

所以,现在尝试测试你的听众,如果他是工作或不运行这个命令:

php app/console container:debug | app_user.registration_complet 

,你会得到这个消息在控制台上:

[container] Information for service sdz_user.registration_complet Service Id sdz_user.registration_complet Class Acme\CovoiturageBundle\Services\RegistrationConfirmListener Tags - kernel.event_subscriber () Scope container Public yes Synthetic no Lazy no Synchronized no Abstract no Done.

我没有提供登录解决方案。这个注册解决方案。我认为这将是相同的逻辑。