Sarah Richardson
Sarah Richardson

Reputation: 873

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.

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. which is in the directory vendor/bwt/vehicle_tracking/src/vendors tracking.interface and tracking.class are in the same directory

<?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

 <?php

// autoload_namespaces.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    //...
    '' => array($vendorDir . '/bwt/vehicle_tracking/src/vendors'),
);

Upvotes: 4

Views: 12531

Answers (2)

Sarah Richardson
Sarah Richardson

Reputation: 873

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

Upvotes: 3

Cerad
Cerad

Reputation: 48865

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

"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.

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

I only tested a fake FM.php class. That fact that there are include statements in there and some other strange code might generate additional errors.

Upvotes: 1

Related Questions