2012-04-10 5 views
1

뉴스 레터 가입 작품을 얻고 싶습니다. 내가 사용하고있는 논리는 사용자가 자신의 전자 메일을 제출할 때입니다. 이미 전자 메일을 사용하여 사이트에 등록 된 사용자가 있는지 확인합니다. 그렇다면 그가 뉴스 레터에 가입하고 적절한 구독을하는지 확인합니다. 이메일이 등록 된 사용자 목록에 없으면 익명 사용자를위한 뉴스 레터 표가 있습니다. 그가 가입했는지 여부를 확인하고 필요한 조치를 취합니다. 나는이 컨트롤러 액션을 호출하는 아약스를 사용하고Symfony2 : 뉴스 레터 구독

/** 
* @Route("/newsletter/", name="site_newsletter") 
* @return array 
*/ 
public function newsletterSubscriptionAction(httpRequest $request) 
{ 
    $email = $request->request->get('email'); 

    try { 

     $email = $request->request->get('email'); 
     $em = $this->getDoctrine()->getEntityManager(); 
     $query = $em->createQuery(
      'SELECT u FROM MyBundle:User u WHERE u.email = :email' 
     )->setParameter('email', $email); 

     $user = $query->getSingleResult(); 

     if(!is_object($user)){ //this means anonymous user not registered to site 
      $em = $this->getDoctrine()->getEntityManager(); 
      $query = $em->createQuery(
       'SELECT n FROM MyBundle:Newsletter n WHERE n.email = :email AND n.isSubscribed = 1' 
      )->setParameter('email', $email); 

      $record = $query->getSingleResult(); 

      if($record){ 
       $msg = "You are already subscribed!"; 
      }else{ 
       $newsletter = new Newsletter(); 
       $newsletter->setEmail($email); 
       $newsletter->setIsSubscribed(true); 

       $em = $this->getDoctrine()->getEntityManager(); 
       $em->persist($newsletter); 
       $em->flush(); 

       $msg = "You have been subscribed to our newsletter! Thank You."; 
      } 
     }else{ 
      if($user->getNewsletterSubscription()){ 
       $msg = "You are already subscribed!"; 
      }else{ 
       $user->setNewsletterSubscription(1); 
       $em = $this->getDoctrine()->getEntityManager(); 
       $em->flush();      
       $msg = "You have been subscribed to our newsletter! Thank You."; 
      }     
     } 

    } 
    catch (\Exception $e) { 
     $msg = 'Some problem occured. Please try again later'; 
    } 

    if ($this->getRequest()->isXmlHttpRequest()) { 
     return new \Symfony\Component\HttpFoundation\Response($msg); 
     return array('msg' => $msg); 
    } 

} 

:

여기 내 컨트롤러 액션 코드입니다. 이것은 작동하지 않습니다. 아약스로 돌아가는 데이터는 리디렉션 된 로그인 페이지입니다.

나는 사용자를 관리하는 방법으로 FOSUSerBundle을 사용하고 있습니다. 이 문제가 발생 했습니까? 그것을 구현하는 가장 좋은 방법은 무엇입니까?

미리 감사드립니다.

답변

2

구독 경로를 사용할 수있는 권한을 anon 사용자에게 부여해야하는 것처럼 들립니다.

security: 
    access_control: 
     - { path: ^/subscribe, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
+0

당신을 감사합니다 당신의 security.yml 파일에서

는 같은 것을 추가! 그것은 효과가 있었다. – VishwaKumar

+0

안녕하세요. 2000 명의 reps 이상 나를 밀었다. 감사. – Cerad