Symfony 4 - 服务注入只能在没有缓存的情况下工作

Symfony 4 - Service injection only works without cache

我是 Symfony 的新手,遇到了一个恼人的问题。我尝试使用 FOSRestBundle 构建一个 API,一个操作需要我创建的服务,但服务注入仅在我删除 var/cache/dev 文件夹时有效。如果缓存在这里,即使在 NGINX 和 PHP-FPM 中,我也只会收到 502 错误,完全没有任何细节。如果你有想法,这是我的代码:

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);
}

App\Service\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'

谢谢!

修改您的 nginx 配置和 change/set 以下指令:

proxy_buffer_size   128k;
proxy_buffers   4 256k;
proxy_busy_buffers_size   256k;

之后重启nginx:

service nginx restart

来源here.

通过升级到 Symfony 4.2 并将我的 Nginx 配置更改为解决:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
}