繁体   English   中英

使用symfony从twig文件中调用服务

[英]call service from twig file with symfony

我的代码如下:

服务:

<?php

namespace Core\Bundle\CoreBundle\Services;

use Core\Bundle\CoreBundle\Entity\Industry;
use Core\Bundle\CoreBundle\Entity\ExpoAdmin;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Config\Definition\Exception\Exception;

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

}

命名空间Core \\ Bundle \\ CoreBundle的service.yml

core.distance_calculation:
            class: Core\Bundle\CoreBundle\Services\calculationDistance
            arguments:  ["@router", "@security.context", "@session", "@service_container", "@doctrine.orm.entity_manager"]

来自app / config的config.yml

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

我从这个位置的twig文件中调用了这个服务 -

namespace Myshop\Bundle\FrontendBundle\Controller;

我的twig文件代码如下调用以上服务:

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

但它给我的错误如下,

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

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

我所看到的是服务中的类名是calculateMilesServices ,你在核心包上定义它作为calculationDistance

错过类型/错过的比赛很常见我的朋友:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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