2013-11-26 15 views
0

我对Symfony2非常陌生,设计一个简单的登录系统。用户类, 路由器,控制器类一切工作正常。我坚持 userRepository类。

我的控制器部分是:

public function loginProcessAction(Request $request){ 

    if($request->getMethod() == "POST") { 

     $username = $request->get('username'); 

     $password = $request->get('password'); 

     $em= $this->getDoctrine()->getEntityManager(); 
     $repository = $em->getRepository("LoginLoginBundle:Student"); 

     $user = $repository->findOneBy(array('username'=>$username, 
       'password'=>$password)); 

     if($user){ 
      return $this->render('loginSuccess twig page') ; 
     } 
     else{ 
      return $this->render('error twig page') ; 
     } 


    } else{ 
     return $this->render("login error page"); 
    } 
} 

How to define findOneBy(username, password) function in reopository class. 

回答

1

这是不使用Symfony2的时候来处理身份验证的最佳方式。看看集成了Symfony2的the Security component

所以检查security documentationHow Security Works: Authentication and Authorization一部分,所有你需要实现/配置是Firewalls来处理身份验证和 Access Controls授权。

但是...

这里有一个答案共同的问题:如何将一个定义findOneBy(参数1,参数)为给定的资源库类的功能?

首先,你的实体映射到相应的存储库作为跟随,

/* 
* @ORM\Entity(repositoryClass="YourNamespace\YourBundle\Entity\yourRepository") 
*/ 
class YourEntity 
{ 
    // ... 
} 

你应该再添加映射库类,并实现一个findOneBy(parameter1, parameter2)方法。

然后您可以将控制器如下内访问这个类,

$em= $this->getDoctrine()->getManager(); 
    $yourEntityInstance = $em->getRepository("yourNamespaceYourBundle:YourEntity") 
       ->findOneBy($parameter1, $parameter2); 
+0

以及如何定义findOneBy($参数1,$参数2)在库类的功能。 – Swass

+0

您已经有了一个可以使用的本地findOneBy()方法,所以我不太了解重写它的必要性。除非你需要改变它的行为方式? –