[英]Attempted to load class from namespace. Did you forget a "use" statement for another namespace? with vendor php namespace
I am trying to load a php namespace into my symfony project but keep getting the following error at runtime.我正在尝试将 php 命名空间加载到我的 symfony 项目中,但在运行时不断收到以下错误。
Attempted to load class "FM" from namespace "VehicleTracking\Src\Vendors\FM".
Did you forget a "use" statement for another namespace?
The controller that it is being called from被调用的控制器
namespace BWT\FMBundle\Controller;
use VehicleTracking\Src\Vendors\FM\FM;
class FMController extends Controller
{
/**
* @Route("/fuel_data", name="fuelData")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function fuelDataAction(Request $request)
{
//...
$tripProcesses = new FM(); //<-this is the line where I get the error
$_results = $tripProcesses->getTripWithTotals($form->get('fleetNo')->getData(), $form->get('startDate')->getData(), $form->get('endDate')->getData());
}
}
The FM.php file. FM.php 文件。 which is in the directory vendor/bwt/vehicle_tracking/src/vendors tracking.interface and tracking.class are in the same directory
这是在目录 vendor/bwt/vehicle_tracking/src/vendors tracking.interface 和 tracking.class 在同一目录中
<?php
namespace VehicleTracking\Src\Vendors\FM;
// : Includes
include_once (dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . 'tracking.interface');
include_once (dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . 'tracking.class');
// : End
use VehicleTracking\Src\Vendors\Vendors as Vendors;
use VehicleTracking\Src\Vendors\TrackingInterface as TrackingInterface;
class FM extends Vendors\Vendors implements TrackingInterface\TrackingInterface
{
public function getTrackingData()
{...}
}
autoload_namespace.php autoload_namespace.php
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
//...
'' => array($vendorDir . '/bwt/vehicle_tracking/src/vendors'),
);
As a rule you should avoid editing anything under vendor.通常,您应该避免在供应商下编辑任何内容。 Your changes will be lost.
您的更改将丢失。 In this case you can edit your projects composer.json file
在这种情况下,您可以编辑您的项目 composer.json 文件
"autoload": {
"psr-4": {
"": "src/",
"VehicleTracking\\Src\\Vendors\\FM\\": "vendor/bwt/vehicle_tracking/src/vendors"
},
After making changes, run composer dump-autoload to update the autoload stuff.进行更改后,运行 composer dump-autoload 来更新自动加载的内容。
The path I gave is based on your question, at least that was the intent.我给出的路径是基于你的问题,至少这是意图。 It assumes that FM.php is located directly under vendor/bwt/vehicle_tracking/src/vendors
它假设 FM.php 直接位于 vendor/bwt/vehicle_tracking/src/vendors 下
I only tested a fake FM.php class.我只测试了一个假的 FM.php 类。 That fact that there are include statements in there and some other strange code might generate additional errors.
那里有包含语句和一些其他奇怪的代码的事实可能会产生额外的错误。
We eventually solved this by adding我们最终通过添加解决了这个问题
"autoload" : {
"psr-4" : {
"Vendors\\" : "src/"
}
},
to the composer.json of the external package and changed the namespace of the classes to namespace Vendors;
到外部包的 composer.json 并将类的命名空间更改为命名
namespace Vendors;
so that it would be the same as the directory.以便它与目录相同。
I have to run composer update -o
to optimise the autoloader otherwise I keep getting these errors.我必须运行
composer update -o
来优化自动加载器,否则我会不断收到这些错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.