【发布时间】:2017-05-04 05:55:16
【问题描述】:
我的代码如下:
服务:
<?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"
我已经从我的这个位置的树枝文件中调用了这个服务 -
namespace Myshop\Bundle\FrontendBundle\Controller;
我的树枝文件代码如下调用上述服务:
{{ 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 -
是的,就是问题。谢谢
-
现在可以用了吗?
-
是的,它正在工作:)