用Symfony从Twig文件中调用服务



我的代码如下:

服务:

<?php
namespace CoreBundleCoreBundleServices;
use CoreBundleCoreBundleEntityIndustry;
use CoreBundleCoreBundleEntityExpoAdmin;
use DoctrineCommonCollectionsCriteria;
use DoctrineORMEntityManager;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
use SymfonyComponentSecurityCoreSecurityContext;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentRoutingRouter;
use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentDependencyInjectionContainer;
use SymfonyComponentHttpFoundationCookie;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentConfigDefinitionExceptionException;
class calculateMilesServices extends Exception {
    protected $router;
    protected $security;
    protected $session;
    protected $container;
    protected $entityManager;
    protected $em;
    public $redirectResponse;
    public function __construct(Router $router, SecurityContext $security, Session $session, Container $container, $entityManager) {
        $this->router = $router;
        $this->security = $security;
        $this->session = $session;
        $this->container = $container;
        $this->entityManager = $entityManager;
        $this->em = $container->get('doctrine')->getManager();
    }
    public function calculationDistance($lat, $long, $language,$commerce) {
        if (!empty($lat) && !empty($long)) {
            $localLanguage = $language;
            $distance = $this->haversineGreatCircleDistance($lat, $long, $commerce->getCoordy(), $commerce->getCoordx());
            $km = round($distance / 1000, 2);
            if ($localLanguage == 'es') {
                $unit = 'km';
                $distance = round(($km / 1000), 2);
            } elseif ($localLanguage == 'en') {
                $unit = 'mi';
                $distance = round(($km / 1.609344), 2);
            } else {
                $unit = 'km';
                $distance = round(($km / 1000), 2);
            }
            $actualdistance = $distance . " " . $unit;
        } else {
            $actualdistance = '';
        }
        return $actualdistance;
    }
    public function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) {
        // convert from degrees to radians
        $latFrom = deg2rad($latitudeFrom);
        $lonFrom = deg2rad($longitudeFrom);
        $latTo = deg2rad($latitudeTo);
        $lonTo = deg2rad($longitudeTo);
        $latDelta = $latTo - $latFrom;
        $lonDelta = $lonTo - $lonFrom;
        $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
                                cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
        return $angle * $earthRadius;
    }
}

service.yml命名空间core bundle corebundle

core.distance_calculation:
            class: CoreBundleCoreBundleServicescalculationDistance
            arguments:  ["@router", "@security.context", "@session", "@service_container", "@doctrine.orm.entity_manager"]

config.yml来自app/config

twig:
 globals:
  distanceCalculation: "@core.distance_calculation"

我已经从这个位置的树枝文件来调用此服务 -

namespace MyshopBundleFrontendBundleController;

我的树枝文件代码如下要在上述服务:

 {{ distanceCalculation.calculationDistance(lat, long, localLanguage,data) }}

,但它给我带来了以下错误的错误,

ClassNotFoundException in appDevDebugProjectContainer.php line 1469:
Attempted to load class "calculationDistance" from namespace "CoreBundleCoreBundleServices".
Did you forget a "use" statement for another namespace?

任何人可以帮助我解决它吗?

我看到的是您在服务中的班级名称是 calculateMilesServices,并且在核心捆绑包上定义为 calculationDistance

小姐类型/错过匹配是我的朋友的常见:)

最新更新