简体   繁体   English

试图从命名空间加载类。 您是否忘记了另一个命名空间的“use”语句? 使用供应商 php 命名空间

[英]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.

相关问题 尝试从全局命名空间加载 class “DOMDocument”。 您是否忘记了“使用”声明? - Attempted to load class "DOMDocument" from the global namespace. Did you forget a "use" statement? 尝试从全局命名空间加载类“Memcached”。 您是否忘记了“使用”语句? - Attempted to load class “Memcached” from the global namespace. Did you forget a “use” statement? 试图从全局名称空间中加载类“ Thread”。 您是否忘记了“使用”声明? - Attempted to load class “Thread” from the global namespace. Did you forget a “use” statement? Symfony2尝试从全局名称空间加载类。 您是否忘记了“使用”声明? - Symfony2 Attempted to load class from the global namespace. Did you forget a “use” statement? 尝试从全局命名空间加载类“PHPUnit_Framework_TestCase”。 您是否忘记了“使用”声明? - Attempted to load class "PHPUnit_Framework_TestCase" from the global namespace. Did you forget a "use" statement? 尝试从命名空间“DoctrineExtensions \ Query \ Mysql”加载类“Month”。您是否忘记了另一个命名空间的“use”语句 - Attempted to load class “Month” from namespace “DoctrineExtensions\Query\Mysql”. Did you forget a “use” statement for another namespace 尝试从命名空间加载类是否忘记了另一个命名空间的“use”语句? - Attempted to load class from namespace did you forget a “use” statement for another namespace? 尝试从命名空间“Symfony\\WebpackEncoreBundle”加载类“WebpackEncoreBundle”。 您是否忘记了另一个命名空间的“use”语句? - Attempted to load class "WebpackEncoreBundle" from namespace "Symfony\WebpackEncoreBundle". Did you forget a "use" statement for another namespace? 尝试从命名空间“knp \\ Bundle \\ PaginatorBundle”加载类“knpPaginatorBundle”。你忘记了另一个命名空间的“use”语句吗? - Attempted to load class “knpPaginatorBundle” from namespace “knp\Bundle\PaginatorBundle”.Did you forget a “use” statement for another namespace? 您是否忘记了另一个名称空间的“使用”语句? - Did you forget a “use” statement for another namespace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM