我正在研究一个项目,我必须使用javascript来检测屏幕分辨率,视口......
我正在使用Symfony 2.8,我创建了一个REST API,用于将(带有ajax的客户端)屏幕设置发布到服务器端(并将其放在Doctrine实体中)。
我正在测试API:
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"screen":[{"id": 1,"resolution": "1920x1080", "viewport": "60x60", "device_ar": "1.5", "px_ratio": "1.00", "zoom": "8%"}]}' myurl/api/screen
我有一个很好的没有找到POST的路线。
这是我的routing.yml:
screen:
type: rest
prefix: /api
resource: MyBrowserBundle\Controller\ScreenController
methods: [POST]
和我的屏幕控制器:
class ScreenController extends FOSRestController
{
/**
* @var $resolution, $viewport, $device_ar, $px_ratio, $zoom
* @Post()
*/
public function postScreenAction($resolution, $viewport, $device_ar, $px_ratio, $zoom)
{
$userpost_screen = new Screen();
$userpost_screen->setResolution($resolution);
$userpost_screen->setViewport($viewport);
$userpost_screen->setDeviceAr($device_ar);
$userpost_screen->setPxRatio($px_ratio);
$userpost_screen->setZoom($zoom);
$em = $this->getDoctrine->getManager();
$em->persist($userpost_screen);
$em->flush();
}
}
我对fosrestbundle的配置:
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: 'force'
formats:
xml: true
json : true
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
routing_loader:
default_format: json
我从今天早上开始研究这个问题,我仍然找不到发生的事情,一点点帮助可能有用:)
感谢
答案 0 :(得分:0)
好的,所以我将控制器更改为:
public function postScreenAction(Request $request)
{
$userpost_screen = new Screen();
$em = $this->getDoctrine()->getManager();
$params = array();
$content = $this->get("request")->getContent();
if (!empty($content)) {
$params = json_decode($content, true);
}
$userpost_screen->setResolution($params["screen"]["resolution"]);
$userpost_screen->setViewport($params["screen"]["viewport"]);
$userpost_screen->setDeviceAr($params["screen"]["device_ar"]);
$userpost_screen->setPxRatio($params["screen"]["px_ratio"]);
$userpost_screen->setZoom($params["screen"]["zoom"]);
$em->persist($userpost_screen);
$em->flush();
}
测试:
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"screen":{"resolution": "1920x1080", "viewport": "60x60", "device_ar": "1.5", "px_ratio": "1.00", "zoom": "8%"}}' dev/~bgonckel/web/symfony/MyBrowser/web/app_dev.php/api/screen
它运作良好:)