我仔细阅读了这篇文章:https://github.com/schmittjoh/serializer/issues/77但是没有找到任何方法在JSON中为JOS序列化器的FOS Rest包中序列化空值(意味着只显示Doctrine的关键)对象,即使它为null).
我在composer.json中使用以下配置
"jms/serializer-bundle": "0.12.*@dev",
"friendsofsymfony/rest-bundle": "0.13.*@dev",
Run Code Online (Sandbox Code Playgroud)
JMS序列化程序配置
#jms-serializer
jms_serializer:
visitors:
json:
options: 0 # json_encode options bitmask
serialize_null: true
Run Code Online (Sandbox Code Playgroud)
或者FOS Rest bunde配置
fos_rest:
view:
serialize_null: true
Run Code Online (Sandbox Code Playgroud)
不行.我没有使用视图我是"view_response_listener:'force'"所以如果可以提供配置中的解决方案,那将有所帮助,谢谢.
我正在使用Symfony2 + FOSRest软件包开发REST api.
我想知道是否有任何方式app_dev.php从浏览器(对应于Accept: text/html,application/xhtml+xml标题)调用dev模式()中的api以"指定格式"显示响应,用symfony提供的探查器包装在html中.
它允许直接在浏览器中调试对api的调用.
编辑:我不想调试HTTP请求,而是整个过程(路由匹配,涉及的数据库查询等).这就是我想要访问symfony探查器的原因.
这个问题解决了,因为我在我的代码中犯了一个非常愚蠢的错误,与CORS或任何东西无关.
如果你想要阅读这个问题,请注意它有一个有效的CORS配置,如果你想要一个很好的例子.
(注意结束)
我有一个多域Symfony应用程序,后端部分由于某些原因使用Web服务而不是经典表单更新数据.
后端:back.mydomain.dev Webservices域:api.mydomain.dev
我使用jQuery对这些web服务进行AJAX调用,如果我想修改或创建对象,我也发送AJAX请求,对象与Doctrine实体合并并持久化.
我已经争取了整整一年的时间来制作GET,PUT,POST和DELETE请求,这些请求可以在这个应用程序上正常工作,并且因为它们位于不同的域中,我被迫在不同的环境中设置CORS.
所有jQuery AJAX请求如下所示:
ajaxObject = {
url: 'http://api.mydomain.dev/' + uri,
type: method, // Can be GET, PUT, POST or DELETE only
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
contentType: "application/json",
jsonp: false,
data: method === 'GET' ? data : JSON.stringify(data) // Here, "data" is ALWAYS containing a plain object. If empty, it equals to "{}"
};
// ... Add …Run Code Online (Sandbox Code Playgroud) 我一直使用Symfony2,FOSRESTBundle和Vichuploader为Instagram等照片共享应用程序编写API进行文件上传.
我能够解决GET和POST请求,但我找不到如何附加到POST请求的示例,实际图像,以便在我的情况下,Vichuploader可以抓住它并帮助我完成文件上传.
顺便说一句,我可以使用通过使用普通表单提到的堆栈上传文件而不会出现问题.
有人可以清楚地解释如何使用FOSRest为REST请求配置路由吗?每个教程似乎都有不同的做法.
我的控制器:
<?php
namespace Data\APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DatasetController extends Controller{
protected function postDatasetAction(Request $request){
//Query here
}
Run Code Online (Sandbox Code Playgroud)
URL应如下所示:Symfony/web/app_dev.php/api/dataset.所以我认为路线应该是......
应用程序/配置/ routes.yml
data_api:
resource: "@DataAPIBundle/Resources/config/routing.yml"
prefix: /api
type: rest
Run Code Online (Sandbox Code Playgroud)
和....
数据/ APIBundle /资源/配置/ routing.yml中
data_query:
type: rest
pattern: /dataset
defaults: {_controller: DataAPIBundle:Dataset:datasetAction, _format: json }
requirements:
_method: POST
Run Code Online (Sandbox Code Playgroud) 我正在使用FOSRestBundle,并且想知道是否可以使用注释验证空查询参数?
例如打电话时:/comments/1因为这两个抛出一个异常dealId,并source查询参数尚未设置.
但是,/comments/1?dealId=1&source=即使该source值未设置并且与注释中概述的正则表达式不匹配,调用也可以.
控制器功能:
/**
* Get a single comment.
*
* @Annotations\QueryParam(name="dealId", requirements="\d+", strict=true, description="The deal the comments belong to.")
* @Annotations\QueryParam(name="source", requirements="(forum|blog)", strict=true, description="The source of the comments.")
*
* @Annotations\View()
*
* @Annotations\Get("/comments/{id}", requirements={"id" = "\d+"})
*
*/
public function getCommentAction(Request $request, ParamFetcherInterface $paramFetcher, $id)
{
$dealId = $paramFetcher->get('dealId');
$source = $paramFetcher->get('source');
// TODO: Implement
return [ 'id' => $id, 'dealId' => $dealId, 'source' => $source ]; …Run Code Online (Sandbox Code Playgroud) 编辑:请参阅下面的我自己的解决方案,在撰写本文时,它正在运作但不完美.我会喜欢一些批评和反馈,如果我把一些我觉得非常可靠的东西放在一起,那么我会为面临同样挑战的其他人制作一篇howto博客文章.
几天来我一直在努力奋斗,如果我走在正确的道路上,我希望有人能告诉我.
我有一个带有FOSRestBundle webservice的系统,我正在使用FOSUserBundle和HWIOAuthBundle来验证用户.
我想为webservice设置无状态api密钥身份验证.
我已经阅读了http://symfony.com/doc/current/cookbook/security/api_key_authentication.html,这看起来很简单,我已经安装了UecodeApiKeyBundle,这似乎只是本书页面的一个实现.
我的问题是n00b ...现在怎么样?书籍页面和包都涵盖了通过API密钥对用户进行身份验证,但不涉及日志用户流,生成API密钥,允许用户注册等.我真正想要的是用于登录的简单API端点,注册,以及我的应用开发者可以使用的注销.像/ api/v1/login等等.
我想我可以处理注册....虽然登录让我感到困惑.根据一些额外的阅读,在我看来,我需要做的登录是这样的:
在api/v1/login创建一个接受POST请求的控制器.请求看起来像{_username:foo,_ password:bar}或类似{facebook_access_token:foo.或者,facebook登录可能需要不同的操作,例如/ user/login/facebook,并且只需重定向到HWIOAuthBundle路径}.
如果请求包含_username和_password参数,那么我需要将请求转发到login-check(我不确定这个.我可以自己处理这个表单吗?或者,我应该手动检查用户名和密码数据库?)
添加一个登录事件监听器,如果用户认证成功,则为用户生成一个api密钥(当然,如果我自己没有检查它,这是必要的)
在POST请求的响应中返回API密钥(这会打破post-redirect-get策略,但是我没有看到任何问题)我认为这消除了上面列出的重定向登录检查选项.
你可能会看到我很困惑.这是我的第一个Symfony2项目,关于安全的书页很简单......但似乎掩盖了一些细节,这使我不确定要采取的方法.
提前致谢!
================================================== ===========
编辑:
我已经安装了API密钥身份验证,与相关的相关菜谱文章完全相同:http://symfony.com/doc/current/cookbook/security/api_key_authentication.html
为了处理用户的登录,我创建了一个自定义控制器方法.我怀疑这是完美的,我很乐意听到一些关于如何改进它的反馈,但我相信我正走在正确的道路上,因为我的流程正在发挥作用.这是代码(请注意,还在开发的早期......我还没有看过Facebook登录,只有简单的用户名/密码登录):
class SecurityController extends FOSRestController
{
/**
* Create a security token for the user
*/
public function tokenCreateAction()
{
$request = $this->getRequest();
$username = $request->get('username',NULL);
$password = $request->get('password',NULL);
if (!isset($username) || !isset($password)){
throw new BadRequestHttpException("You must pass username and password fields");
}
$um = $this->get('fos_user.user_manager');
$user = $um->findUserByUsernameOrEmail($username);
if (!$user instanceof \Acme\UserBundle\Entity\User) …Run Code Online (Sandbox Code Playgroud) 我得到了例外:
无法找到模板""
其他类似的问题没有帮助; 奇怪的是它工作正常然后突然开始给我这个例外.
作曲家:
Run Code Online (Sandbox Code Playgroud)"friendsofsymfony/rest-bundle": "0.13.*@dev", "jms/serializer-bundle": "0.12.*@dev",
我正在关注自动路由生成:单个RESTful控制器(用于简单资源)
配置:
fos_rest:
format_listener: true
routing_loader:
default_format: json
view:
view_response_listener: 'force'
serializer:
serialize_null: true
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
Run Code Online (Sandbox Code Playgroud)
路由:
sectors:
type: rest
prefix: /{v}
resource: JJ\MainBundle\Controller\SectorsController
Run Code Online (Sandbox Code Playgroud)
控制器:
/**
* @RouteResource("Sector")
*/
class SectorsController extends Controller
...
public function cgetAction()
{
return $this->getSectorIndustryRepo()->findAll();
}
Run Code Online (Sandbox Code Playgroud) 我的相应配置是
fos_rest:
view:
view_response_listener: force
sensio_framework_extra:
view:
annotations: false
Run Code Online (Sandbox Code Playgroud)
将路线指定为,真的很烦人
@Route("/jobs", defaults={ "_format" = "json" })
Run Code Online (Sandbox Code Playgroud)
每次.
那么可以在默认情况下将其指定在某个地方吗?
PS:
如果我删除defaults={ "_format" = "json" }并调用/jobs端点我得到一个例外
Unable to find template "APIBundle:Jobs:post.html.twig".
Run Code Online (Sandbox Code Playgroud)
PPS:
routing_loader:
default_format: json
Run Code Online (Sandbox Code Playgroud)
将无法工作,因为它仅用于自动路线生成.
我已将FOSRestBundle配置如下:
#FOSRestBundle
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener:
rules:
- { path: ^/, priorities: [ json, html ], fallback_format: ~, prefer_extension: true }
media_type:
version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'
body_converter:
enabled: true
validate: true
view:
mime_types:
json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
view_response_listener: 'force'
formats:
xml: false
json: true
templating_formats:
html: true
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
Run Code Online (Sandbox Code Playgroud)
我在控制器上有这个:
namespace PDI\PDOneBundle\Controller\Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\Get;
class RepresentativeRestController extends FOSRestController
{
/**
* Get …Run Code Online (Sandbox Code Playgroud) fosrestbundle ×10
symfony ×10
php ×6
rest ×2
ajax ×1
api ×1
cors ×1
jquery ×1
json ×1
symfony-2.3 ×1
symfony-2.6 ×1