Symfony 2 - FOSUserBundle - 如何在API中集成

man*_*oon 4 api mobile symfony fosuserbundle fosrestbundle

我正在研究一个新项目symfony 2.我正在同时学习这个框架.对于用户管理,我使用捆绑FOSUserBundle.我的项目运作良好,我可以登录,注册,注销和所有其他命令.

问题是,我想制作智能手机应用程序,它将使用我的Symfony应用程序的API.在应用中,用户必须登录或注册.是否也可以为API使用FOSUserBundle方法?

我研究了另一个非常适合制作API的软件包,它是FOSRestBundle.如果没有解决方案,你认为我将不得不创建我自己的用户方法,如:

    /api/login
    /api/registrer 
Run Code Online (Sandbox Code Playgroud)

然后,在这个方法里面,我重定向到FOSUserBundle方法?我很想知道什么是最好的,最干净的登录方式,并通过智能手机注册FOSUserBundle,所以使用API

谢谢

the*_*wbb 5

我也有这个问题.我找到了最好的解决方案就是这个

use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class YourController extends Controller{
//Other Methods..

public function loginAction(Request $request){
    try{
        $token = $this->get('security.authentication.manager')->authenticate(new UsernamePasswordToken('username', 'password', 'firewall'));
        $this->get('security.context')->setToken($token);
    }
    catch(BadCredentialsException $e){
        return new Response("Bad credentials", 403);
    }
    return new Response("success");
}
}
Run Code Online (Sandbox Code Playgroud)


man*_*oon 2

我使用了 FOSRestBundle。

该捆绑包功能非常强大且易于实施。文档相当完整。

FOSRestBundle的github链接在这里

希望有帮助