Я новичок в Symfony, и у меня возникла досадная проблема.Я пытаюсь создать API с FOSRestBundle , для одного действия требуется созданная мной служба, но внедрение службы работает только при удалении папки var/cache/dev
.Если кеш здесь, я получил только ошибку 502, абсолютно без подробностей, даже в NGINX и PHP-FPM.Если у вас есть идея, вот мой код:
App \ Controller \ PlaceController
<?php
namespace App\Controller;
use ...;
class PlaceController extends ApiController {
/** @var EntityManagerInterface */
private $entityManager;
/** @var PlaceRepository */
private $placeRepository;
/** @var ValidatorInterface */
private $validator;
/** @var GeocodingInterface */
private $geocoder;
public function __construct(EntityManagerInterface $entityManager, PlaceRepository $placeRepository, ValidatorInterface $validator, GeocodingInterface $geocoder) {
parent::__construct();
$this->entityManager = $entityManager;
$this->placeRepository = $placeRepository;
$this->validator = $validator;
$this->geocoder = $geocoder;
}
/**
* @ParamConverter("bodyPlace", converter="fos_rest.request_body")
* @param Place $bodyPlace
* @return \Symfony\Component\HttpFoundation\Response
*/
public function postPlacesAction(Place $bodyPlace) {
$errors = $this->validator->validate($bodyPlace);
if($errors->count() === 0) {
$place = new Place();
$place->setName($bodyPlace->getName());
$place->setAddress($bodyPlace->getAddress());
$coordinates = $this->geocoder->geocode($place->getAddress());
if(!is_null($coordinates)) {
$place->setLongitude($coordinates->getLongitude());
$place->setLatitude($coordinates->getLatitude());
}
$this->entityManager->persist($place);
$this->entityManager->flush();
return $this->renderJson($place);
}
return $this->renderJson((string)$errors);
}
}
App \ Utils \ GeocodingInterface
<?php
namespace App\Utils;
interface GeocodingInterface {
public function geocode(string $address);
public function reverseGeocode(float $latitude, float $longitude);
}
Приложение \ Сервис \ OpenStreetMapGeocoder
<?php
namespace App\Service;
use ...;
class OpenStreetMapGeocoder implements GeocodingInterface {
/** @var HttpClient */
private $httpClient;
/** @var Nominatim */
private $provider;
/** @var StatefulGeocoder */
private $geocoder;
public function __construct()
{
$this->httpClient = new Client();
$this->provider = new Nominatim($this->httpClient, 'https://nominatim.openstreetmap.org', 'fr');
$this->geocoder = new StatefulGeocoder($this->provider, 'fr');
}
/**
* Transform an address to a longitude and a latitude.
* @param string $address
* @return \Geocoder\Model\Coordinates|null
*/
public function geocode(string $address)
{
try {
$query = GeocodeQuery::create($address);
$result = $this->geocoder->geocodeQuery($query);
return $result->first()->getCoordinates();
} catch (Exception $e) {
return null;
}
}
public function reverseGeocode(float $latitude, float $longitude)
{
throw new NotImplementedException('Not implemented yet.');
}
}
services.yaml
App\Service\OpenStreetMapGeocoder: ~
App\Utils\GeocodingInterface: '@App\Service\OpenStreetMapGeocoder'
Спасибо!