Vendor Bundle failed to load - php

I searched for hours to solve my problem, but now I just don't know what to check anymore..
I created a new project for composer: https://github.com/Gcob/esvit-ng-table-for-symfony
Everything is fine until it comes to the appKernel.php, I declarate my new freshly downloaded from composer bundle like this: new Gcob\NgTableBundle\GcobNgTableBundle(), but I got an error message:
ClassNotFoundException in AppKernel.php line 23:
Attempted to load class "GcobNgTableBundle" from namespace "Gcob\NgTableBundle".
Did you forget a "use" statement for another namespace?
I don't know exacly how the appKernel finds its bundles, but I know that the namespace is important and the filename too, so my file GcobNgTableBundle.php has the namespace namespace Gcob\NgTableBundle; and the class declaration is class GcobNgTableBundle extends Bundle as it should be.
Is there any place I should tell the kernel that the file GcobNgTableBundle.php exists for vendor bundles? If some one got any idea, please tell me, but don't forget that I tried a lot of stuff ( first time asking question o_O )

You need to change your composer and add an autoload part.
Without it, namespaces can fail. Check documentation for details.
It must be similar to this:
"autoload": {
"psr-4": { "Symfony\\Bundle\\EsvitNgTableBundle\\": "" }
},
Check this one for example.

After a lot of wasted time, I understood :P The answer of mim was in the good direction! I learned here how composer namespaces works. The appKernel in Symfony only loads the composer file "vendor/composer/autoload_namespaces.php" to load FAKE namespaces xD.

Related

Codeception unit tests - error Class 'Dog' not found

I'm using Codeception to TDD the development of a simple application in PHP. I've created my first unit test called DogTest.php with a simple assertion, but it's complaining about not being able to find the Dog class.
I have created a Dog.php file in the root directory, and also placed it under /src, but neither is working. I think this is either a namespace issue or an autoloader issue, but the Codeception documents (and the various TDD guides I've looked at) have this important detail missing.
Can someone please advise on how to get my DogTest to detect the Dog class?
In your composer.json, make sure you have added autoloading configuration. As an example:
"autoload":{
"psr-4":{
"Del\\":"src/"
}
}
Every file in src should have namespace Del. For instance, src/Blank.php would look like:
<?php
namespace Del;
class Blank
{
}
Whereas src/Http/Client.php would have namespace Del\Http.
Once added, run composer dumpautoload to generate the class maps. Your classes should now autoload without problems.
See my Blank starter project with codeception test for more info.
https://github.com/delboy1978uk/blank

Installation of Sonata ClassificationBundle autoload ClassNotFound

I'm try to install Sonata ClassificationBundle on my project in symfony 3.4.
But i can't install it correctly, i thinks i have missing something.
I have pass all step but my application display this message:
Attempted to load class "ApplicationSonataClassificationBundle" from namespace "Application\Sonata\ClassificationBundle".
Did you forget a "use" statement for another namespace?
I have try to add class to autoload with
"psr-4": {
...
"Application\\Sonata\\ClassificationBundle": "src/Application/Sonata/ClassificationBundle",
"Application\\": "/src/Application"
},
Without result same error.
My class was perfectly generate.
How can i do ?
Thanks
Sonata ClassificationBundle Documentation
no need for an extra PSR-4 entry.
Application\\ -> src/Application
is enough, but you should remove the leading / before src.

Symfony - How to override a vendor component which is not in a bundle

I'm searching for override a file which is in the vendor's directory
but is not a bundle.
I'm working with Symfony 2.7
To be more specific, i'm trying to override a method in this file :
vendor/akeneo/pim-community-dev/src/Pim/Component/Catalog/Updater/ProductUpdater.php
And I'd like to do it in a file like :
src/MyApp/Component/Catalog/Updater/ProductUpdater.php
All the documentation I've found is relied to parts of a Bundle.
So, is it even possible to do that ?
If it is, how to do it ?
There is a way to override a file using composer.
You can add in your composer.json under the autoload->psr-4 key something like this.
"autoload": {
"psr-4": {
"Pim\\Component\\Catalog\\Updater\\": "MyApp/Pim/Component/Catalog/Updater"
},
Where the first part is the namespace of the file you want to override and the second part is the new path.
This can create some issues when you add new packages to composer, so make sure you run composer dump-autoload to recreated the autoload order.
i don't really like this method, but it did save me a couple of times to fix files from bundles without actually forking the bundle, and if nothing else helps this can me used as a last resort.
More about composer autoload here
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Hope this helps,
Alexandru Cosoi

Can’t run PHPUnit with Composer on Travis-CI, class not found

I need some help with a problem. I'm trying to execute a simple build with tests on Travis-CI, but its error, saying that it could not found Class:
Fatal error: Class 'com\bitshammer\collection\utils\CollectionUtils' not found in /home/travis/build/BitsHammer/CollectionUtils/test/CollectionUtilsTest.php on line 20
Just for your knowledge, it’s my first project using Composer! What I am doing wrong? Do you guys have any idea? Thanks!
Travis-CI page
Project page on GitHub
I believe your namespaces are wrong for the autoloading.
In composer.json, your autoloading maps the namespace com\bitshammer\ to src/.
You currently have the namespace at com\bitshammer\collection\utils which means your file path for this class would instead needs to be src/collection/utils/CollectionUtils.php instead of src/CollectionUtils.php.
Alternatively you could change the namespace for this class to be com\bitshammer instead of com\bitshammer\collection\utils.

Laravel Custom Trait Not Found

I'm new to traits, but thought I'd give it a try. But, it doesn't seem to load.
I've created a trait within a folder under the Laravel app directory: app\Helpers called CheckPermsAgainstObjectTrait.php
Here is the trait code:
<?php
namespace App\Helpers;
trait CheckPermsAgainstObjectTrait {
function something{}
}
I try to use it in a controller as such:
<?php
namespace App\Http\Controllers;
use this&that;
use App\Helpers\CheckPermsAgainstObjectTrait;
class PolicyController extends Controller{
use CheckPermsAgainstObjectTrait;
}
Classes in that directory load fine. PHPStorm sees the trait fine. I've clear compiled aritsan and dumped autoload. I'm guessing there is something that Laravel doesn't like with the namespacing? I would hope I don't need to do any manual loading in composer -- but I'm having trouble finding any documentation to give me a hint as to what I'm screwing up.
The error:
FatalErrorException in PolicyController.php line 15:
Trait 'App\Helpers\CheckPermsAgainstObjectTrait' not found
Any thoughts?
Did you dump autoload files?
composer dump-autoload
The answer for me was that I had the wrong namespace at the top of my trait file due to working from a Laravel trait as an example. If your trait is in App/Traits/MyTrait.php then make sure your namespace is:
namespace App\Traits;
trait MyTrait
{
// ..
}
Then from the file that's including the trait:
use App\Traits\MyTrait;
class MyClass
{
use MyTrait;
// ..
}
No need to mess with config/app.php, composer.json or autoloading.
I had a very similar problem where I was getting the error Trait 'Tests\CreatesApplication' not found in '.../my_project/tests/TestCase.php' on line 9 while running tests with phpunit because I manually updated my Laravel project and must have missed some changes. This may also happen to anyone creating a trait or class in a unique namespace.
Anyways, #Devon 's comment on the original question pointed me in the right direction.
I was missing the required autoloader configuration in my composer.json file, so I checked what it should be for my version of Laravel (5.4 in this case) from the Laravel github repository.
In this case, I was not calling from a namespace within App\ (which was already in composer.json.)
Here's what I was missing from composer.json:
{
...,
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
...
}
Thanks goes to Devon! Hope this keeps someone else from pulling their hair out.
composer dumpautoload -o
this worked for me
This may happen sometime when your Trait filename is misspelled or incorrect as with the class name. Do check your file name is same as classname
This may happen sometime when your Trait filename is misspelled or incorrect as with the class name. Do check your file name is same as classname

Categories