【问题标题】:Symfony 4 - Service injection only works without cacheSymfony 4 - 服务注入只能在没有缓存的情况下工作
【发布时间】:2018-12-01 23:55:35
【问题描述】:

我是 Symfony 的新手,遇到了一个烦人的问题。我尝试使用 FOSRestBundle 构建 API,其中一项操作需要我创建的服务,但服务注入仅在我删除 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'

谢谢!

【问题讨论】:

    标签: symfony dependency-injection dependencies symfony4


    【解决方案1】:

    修改您的 nginx 配置并更改/设置以下指令:

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

    之后重启nginx:

    service nginx restart
    

    来源here

    【讨论】:

    • 还是不行。我尝试禁用/启用 OPCache,增加memory_consumption。没有任何工作。在我的 nginx 中,我收到以下错误:kevent() reported about an closed connection (54: Connection reset by peer) while reading response header from upstream
    【解决方案2】:

    通过升级到 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 1970-01-01
      • 2021-07-16
      • 2018-01-10
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多