简体   繁体   English

API REST SYMFONY TWIG-AUTH

[英]API REST SYMFONY TWIG - AUTH

I'm working on a REST api with symfony, this will perform tasks related to users, authenticate, list, view details, create, etc. 我正在使用带有symfony的REST api,它将执行与用户,身份验证,列表,查看详细信息,创建等相关的任务。

I have already obtained the authentication but I have doubts, mainly with the TWIG views, since I want to centralize these views and not have to maintain them in each system that connects. 我已经获得了身份验证,但是主要对于TWIG视图,​​我对此表示怀疑,因为我想集中这些视图,而不必在每个连接的系统中维护它们。

What I want to centralize is: 我要集中的是:

  • View of user detail. 用户详细信息视图。
  • View of create / edit user. 创建/编辑用户视图。

Is it okay to place these views in the API? 可以将这些视图放置在API中吗? Is it okay to return a full view from the API? 可以从API返回完整视图吗? How should I do it? 我该怎么办? Since it is returned in JSON format 由于它以JSON格式返回

I am extending from FOSRestController. 我从FOSRestController扩展。

You can make an action which will return JSON or HTML format depending od parameter, so for example you can use JSON format in mobile app and HTML format in some web app. 您可以根据od参数执行返回JSON或HTML格式的操作,例如,可以在移动应用程序中使用JSON格式,而在某些Web应用程序中使用HTML格式。

The possible solution is: 可能的解决方案是:

public function userDetailAction($id, $format)
{
    ...
    ...

    if ($format == 'json') {
        $response = new Response(json_encode($data));
        return $response;
    }

    return $this->render('userDetail.twig', array('userData' => $data));
}

or simpler json syntax in Symfony 3 或Symfony 3中更简单的json语法

...
if ($format == 'json') {
    return $this->json($data);
}
...

Thanks to @vini comment, I implemented it as follows: 感谢@vini评论,我实现了它如下:

...
$view = View::create(array('user' => $user))
          ->setTemplate("EdcorpUserBundle:User:advance.profile.html.twig")
          ->setFormat('html');

return $this->handleView($view);
}

ref: http://symfony.com/doc/master/bundles/FOSRestBundle/2-the-view-layer.html 参考: http : //symfony.com/doc/master/bundles/FOSRestBundle/2-the-view-layer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM