2016-05-29 122 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.

我沒有提供登錄解決方案。這個註冊解決方案。我認爲這將是相同的邏輯。