简体   繁体   中英

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. 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). I have tried to use DoctrineRestDriver

Firstly, I have configured the config.yml file of client-api project:

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:

 /**
 * @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

Where is my problem ? How can I get access from symfony project 1 to symfony project 2 ?

Thanks.

So, you have 2 services. Each of service is responsible for its own entities. And you want service1 to have access directly to DB of service2...

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.

How to do it in better way? Create RestApi Client library for service2 and add it to service1 composer.json file. Dont forget to use authentication (hardcoded API keys or JWT...).

So, answer to your question 'where is problem?' I can say problem is in solution that you want to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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