简体   繁体   English

Symfony:访问另一个 symfony REST API 项目

[英]Symfony: accessing to another symfony REST API project

This is my context:这是我的上下文:

I have two symfony REST API projects, and I want to do a relation between both projects.我有两个 symfony REST API 项目,我想在两个项目之间建立关系。 Each project uses his own database, entities and controllers.每个项目都使用自己的数据库、实体和控制器。

From project 1 (client-api), I need to get access to entities of project 2 (product-api).从项目 1 (client-api),我需要访问项目 2 (product-api) 的实体。 I have tried to use DoctrineRestDriver我曾尝试使用DoctrineRestDriver

Firstly, I have configured the config.yml file of client-api project:首先,我配置了client-api项目的config.yml文件:

doctrine:
dbal:
    default_connection: default
    connections:
        default:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            memory: "%database_memory%"
            path: "%database_path%"
            charset:  UTF8
        product_api:
            driver_class: "Circle\\DoctrineRestDriver\\Driver"
            host: "http://localhost"
            port: 8000
            user:
            password:
            options:
                authentication_class:  "HttpAuthentication"


orm:
    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            mappings:
                AppBundle:
        product_api:
            connection: product_api
            mappings:
                AppBundle:

I want to read a country (id = 1) from the product-api project, so I have created this controller function on client-api project:我想从 product-api 项目中读取一个国家(id = 1),所以我在 client-api 项目上创建了这个控制器函数:

 /**
 * @Route("/countries", name="countries_other_api")
 */
public function getTheCountriesFromOtherApi()
{
    $em = $this->getDoctrine()->getManager('product_api');
    $country = $em->find("AppBundle\Entity\Country", 1);
}

But I'm getting the eror:但我得到了错误:

Class 'AppBundle\\Entity\\Pais' does not exist类“AppBundle\\Entity\\Pais”不存在

Where is my problem ?我的问题在哪里? How can I get access from symfony project 1 to symfony project 2 ?如何从 symfony 项目 1 访问 symfony 项目 2 ?

Thanks.谢谢。

So, you have 2 services.因此,您有 2 个服务。 Each of service is responsible for its own entities.每个服务都对自己的实体负责。 And you want service1 to have access directly to DB of service2...并且您希望 service1 可以直接访问 service2 的 DB ...

My opinion is that you try to solve things in wrong way.我的观点是你试图以错误的方式解决问题。 If you want to get entities from service2 you should use REST API of service2 because you need to reimplement service2 logic in service1.如果您想从 service2 获取实体,您应该使用 service2 的 REST API,因为您需要在 service1 中重新实现 service2 逻辑。

How to do it in better way?如何以更好的方式做到这一点? Create RestApi Client library for service2 and add it to service1 composer.json file.为 service2 创建 RestApi 客户端库并将其添加到 service1 composer.json 文件中。 Dont forget to use authentication (hardcoded API keys or JWT...).不要忘记使用身份验证(硬编码的 API 密钥或 JWT...)。

So, answer to your question 'where is problem?'所以,回答你的问题“问题在哪里?” I can say problem is in solution that you want to implement.我可以说问题在于您要实施的解决方案。

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

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