Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to load class from namespace. Did you forget a "use" statement for another namespace? with vendor php namespace

Tags:

php

symfony

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'),
);
like image 345
Sarah Richardson Avatar asked Sep 01 '25 09:09

Sarah Richardson


2 Answers

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.

like image 184
Sarah Richardson Avatar answered Sep 04 '25 00:09

Sarah Richardson


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.

like image 45
Cerad Avatar answered Sep 04 '25 00:09

Cerad