我正在使用FOSRestbundle实现rest API.现在说对于POST请求我正在获取请求参数,如:
Symfony\Component\HttpFoundation\ParameterBag Object
(
[parameters:protected] => Array
(
[rank] => 12
[city] => 1345
[comment]=> 'safd'
)
)
Run Code Online (Sandbox Code Playgroud)
我的帖子操作代码是:
/**
* @Rest\View
*/
public function newAction(){
$rank= new Rank();
$form = $this->createForm(new RankType(), $rank);
$form->bind($this->getRequest());
if ($form->isValid()) {
//. $user->flush();
$em = $this->getDoctrine()->getManager();
$em->persist($rank);
$em->flush();
$response = new Response();
$response->setStatusCode($statusCode);
$view = View::create()
->setData($rank)
->setFormat('json');
return $this->handleView($view);
}
return $this->handleView(View::create($form, 400));
}
Run Code Online (Sandbox Code Playgroud)
但form->isValid由于设置空值而失败.
After Binding form form->getData() will display :
MyProject\DataBundle\Entity\Rank Object
(
[city:MyProject\DataBundle\Entity\Ranking:private] =>
[rank:MyProject\DataBundle\Entity\Ranking:private] => …Run Code Online (Sandbox Code Playgroud) 当我运行此命令以查看Get方法是否正常工作时,我按照此链接检测到我的终端ERROR 415:不支持的媒体类型中显示此错误.
我要看看我的源代码:
1-首先我设置我的配置文件:
#app/config/config.yml sensio_framework_extra:view:annotations:false
fos_rest:
Run Code Online (Sandbox Code Playgroud)param_fetcher_listener: true body_listener: true format_listener: true view: view_response_listener: 'force'
2-我刚修好路由文件后:
#应用程序/配置/ routing.yml中
Run Code Online (Sandbox Code Playgroud)minn_ads_api: resource: "@MinnAdsAPIBundle/Resources/config/routing.yml" type: rest
3-我还在我的*AdsAPIBundle中设置了路由文件:*
Run Code Online (Sandbox Code Playgroud)#AdsAPIBundle/Resources/config/routing.yml brend: type: rest resource: Minn\AdsAPIBundle\Controller\BrendController
4-在我的AdsAPIBundle下我只插入函数:
#src/BundleApi/Controller/BrendController.php
/**
* @Rest\View
* @Rest\Get("/api/brend/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})
*/
public function getAction($id) {
$repo = $this->getDoctrine()->
getManager()->
getRepository("MinnAdsBundle:Brend");
$brend = $repo->find($id);
return array('brend' => $brend);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Symfony2和FOSRestBundle在RESTFul API中工作.实体使用Gedmo作为Timestampable选项:
use Gedmo\Timestampable\Traits\TimestampableEntity;
Run Code Online (Sandbox Code Playgroud)
我得到了createdAt任何其他专栏:
$obj->getCreatedAt()
Run Code Online (Sandbox Code Playgroud)
然后我将该响应传递给FOSRestBundle:
$respEmail = [
"id" => (string)$entEmail->getId(),
...
"category" => $entEmail->getEmailsCategory(),
"createdAt" => $entEmail->getCreatedAt()
];
$view->setData($respEmail)->setStatusCode(200);
Run Code Online (Sandbox Code Playgroud)
我得到了这个:
{
"id": "5",
...
"category": "sent",
"createdAt": "2015-06-12T11:00:55-0430"
}
Run Code Online (Sandbox Code Playgroud)
我该如何修复日期?我想要类似的东西2015-06-12 11:00:55.有帮助吗?
您好我想动态更改序列化上下文的组.
代码 :
/**
* @Rest\Get("", name="bap_api_space_query")
* @Rest\View(serializerGroups={"Default", "space_dashboard", "dashboard_resource"})
*
* @ApiDoc(resource=true,description="List all spaces this user has access to")
*/
public function queryAction(Request $request)
{
$user = $this->getUser()->reload();
$organization = $user->getOrganization();
// depending the request, remove or add serialized group
// for example $view->setSerializationGroups('dashboard');
return $organization->getSpaces();
}
Run Code Online (Sandbox Code Playgroud)
如代码中所述,我想在控制器中删除或添加组.有办法吗?
我正在一个symfony项目中构建一个rest API,我有4个彼此相关的实体,例如:
例如,当我想获取资源时,我已经安装了FOSRestBundle来构建GET Web服务:
我收到此错误:
消息:“已检测到循环参考(配置的极限:1)。”,
这是我的控制器:
<?php
namespace API\APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
class CartographyController extends Controller
{
/**
* @Rest\View()
* @Rest\Get("/posts")
* @param Request $request
* @return Response
*/
public function getPostsAction(Request $request)
{
$encoder = new JsonEncoder();
$normalizer = new GetSetMethodNormalizer();
$serializer = new Serializer(array($normalizer), array($encoder));
$posts = $this->get('doctrine.orm.entity_manager')
->getRepository('EvalBundle:Post')
->findAll();
return new Response($serializer->serialize($posts, 'json'));
}
/**
* @Rest\View()
* @Rest\Get("/employments") …Run Code Online (Sandbox Code Playgroud) 我不明白 @QueryParam 和 @RequestParam 注释与 FOSRestBundle 之间的区别(例如:https ://symfony.com/doc/master/bundles/FOSRestBundle/param_fetcher_listener.html )。
你有什么办法知道我必须使用哪一个来声明我的 API 参数?
从上周开始我就在尝试让 FOS Auth Server Bundle 与 Symfony4 一起工作。如果我想使用我创建的创建客户端命令,则会出现此错误消息。
编译容器时,“fos_oauth_server.client_manager.default”服务或别名已被删除或内联。您应该将其公开,或者停止直接使用容器并改为依赖项注入。
有人有同样的问题吗?
我正在使用 Symfony 5 开发一个项目。我创建了用户实体,在 security.yaml 上创建了身份验证流程,一切运行良好:如果用户想要访问受保护区域,则会显示登录页面并且身份验证过程有效!超好的!
现在,我想使用 FOSRest Bundle 构建一个 API REST。我创建了一个特定的控制器来公开一些路由:
/**
* @Rest\Route("/api")
*
*/
class APICustomController extends AbstractController
{
...
/**
* @Rest\Get("/shoes")
* @param Request $request
* @Method({"GET"})
*
* @return JsonResponse
*/
public function getShoes(Request $request){
....
return JsonResponse::fromJsonString(array('msg' => 'OK'));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 security.yaml
security:
enable_authenticator_manager: true
encoders:
App\Entity\User:
algorithm: auto
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_VIEWER: ROLE_USER
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用@Groups注释来序列化我的数据,但我只是收到一个空数组。
我试过@Serializer\Expose在我想显示的字段上使用,但随后它们随处可见而忽略了这些组。
从我读过的内容来看,我很确定我应该只需要使用@Groups注释,但我最终得到了这个(有两个计划实体):
{
"schedules": [
{},
{}
]
}
Run Code Online (Sandbox Code Playgroud)
它的工作方式似乎发生了变化,我找到了以前版本 FOSRestBundle 的示例,我只是无法将它们移植到新版本。
据我了解,您可以在序列化程序上下文中设置组并使用它来设置视图上下文,但我对此并不走运。
https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md
View::setSerializationContext 和 View::getSerializationContext 已被删除。将 View::setContext 和 View::getContext 与新的 Context 类一起使用。
前:
Run Code Online (Sandbox Code Playgroud)use JMS\Serializer\SerializationContext; $view = new View(); $context = new SerializationContext(); $view->setSerializationContext($context); $context = $view->getSerializationContext();后:
Run Code Online (Sandbox Code Playgroud)use FOS\RestBundle\Context\Context; $view = new View(); $context = new Context(); $view->setContext($context); $context = $view->getContext();
我花了很长时间来解决所有这些问题,并且非常感谢任何帮助为我指明正确方向的帮助。
除了下面我试图use FOS\RestBundle\Controller\Annotations as Rest; 和
@Rest\View(serializerGroups({"schedule"})注释与控制器上,它有同样的效果,因为我看到现在(即无)。
调度控制器
use FOS\RestBundle\Context\Context;
class ScheduleController extends BaseController
{ …Run Code Online (Sandbox Code Playgroud) 我正在开发一个RESTAPI Symfony 3.现在我想向用户发送一个重置密码的链接.这应该通过向以下内容发送请求来完成:
"/resetpassword/{userEmail}"
Run Code Online (Sandbox Code Playgroud)
如何为用户创建重置密码的链接?我现在已经完成了如何通过我自己发送电子邮件,但不知道如何生成此链接或如何处理链接.
fosrestbundle ×10
symfony ×10
rest ×4
php ×3
api ×1
bind ×1
date ×1
doctrine-orm ×1
entity ×1
forms ×1
symfony-2.1 ×1
symfony5 ×1