我的代码如下:
服务
<?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.ymlcore.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?
任何人都可以帮我解决吗?
答案 0 :(得分:0)
我所看到的是,您的服务中的类名是calculateMilesServices
,您可以在核心包上将其定义为calculationDistance
错过类型/错过的比赛很常见我的朋友:)