简体   繁体   English

symfony3 / php:从api的URL检索属性

[英]symfony3/php: retrieve attribute from url for api

For a datatable, I have to use a REST API. 对于数据表,我必须使用REST API。

My url is for example: http://localhost:8000/trial/1 我的网址例如: http:// localhost:8000 / trial / 1

In this page to make the api call I use the following: 在此页面中,我使用以下命令进行api调用:

url: "rest->{{ path('erp_interventionapi_get') }}",

I need to get the attribut id="1" to make the correct call(find by id in the database), but I don't know how to get it. 我需要获取属性id =“ 1”才能进行正确的调用(在数据库中按ID查找),但是我不知道如何获取它。

I've tried this 我已经试过了

    /**
 * @Rest\Get("/api_i/get")
 */
public function getAction(Request $request)
{
    $id = $request->attributes->get('id');
    $em = $this->getDoctrine()->getManager();
    $trial = $em->find('ErpBundle:Trial', $id);
    return $em->getRepository('ErpBundle:Intervention')->findBy(array('trial' => $trial));
}

but I have this error message: 但我有此错误信息:

The identifier id is missing for a query of ErpBundle\\Entity\\Trial ErpBundle \\ Entity \\ Trial查询缺少标识符ID

The following isn't working either: 以下内容也不起作用:

    /**
 * @Rest\Get("/api_i/get")
 */
public function getAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $trial = $em->find('ErpBundle:Trial', $id);
    return $em->getRepository('ErpBundle:Intervention')->findBy(array('trial' => $trial));
}

I guess it is because it try to retrieve the id from the url /api_i/get and not from the webpage itself. 我猜是因为它尝试从URL / api_i / get而不是从网页本身检索ID。

So is it possible to get the attribute from the url where I make the API call? 那么是否可以从我进行API调用的网址中获取属性?

Excuse me if I make mistakes in my way to say things, I'm still pretty new to this :) 对不起,如果我在说话时犯了错误,我还是很陌生:)

Thanks in advance for your help! 在此先感谢您的帮助!

您需要在路径函数中设置id:

{{ path('erp_interventionapi_get', {id: app.request.attributes.get('id')}) }}

My assumption is you're passing in the Trial object (either as a list or a single one), so when you use Symfony's Twig function path() (note, this is NOT a Twig function, it comes from the Twig bundle for Symfony), you need to pass in the id: 我的假设是您要传入Trial对象(作为列表或单个对象),因此当您使用Symfony的Twig函数path() (请注意,这不是Twig函数,它来自Symfony的Twig捆绑包) ),您需要传递ID:

{{ path('erp_interventionapi_get', { id: trial.id }) }}

Or, alternatively: 或者,或者:

{% for trial in trials %}
    {{ path('erp_interventionapi_get', { id: trial.id }) }}
{% endfor %}

https://symfony.com/doc/current/templating.html#linking-to-pages https://symfony.com/doc/current/templating.html#linking-to-pages

Note, if you're outputting this as JSON for a SPA or widget-based consumption, I would recommend doing that as a client-side router strategy, eg, build the route on the client with serialized data (sans embedded URL). 请注意,如果您将其作为JSON输出以用于SPA或基于窗口小部件的使用,则建议将其作为客户端路由器策略进行操作,例如,使用序列化数据(没有嵌入URL)在客户端上构建路由。 For instance, in Backbone you would tell it how to "get" to a resource like: 例如,在Backbone中,您将告诉它如何“获取”资源,例如:

this.route("trial/:id", "trial", function(id){ ... });

http://backbonejs.org/#Router http://backbonejs.org/#Router

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

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