Sylius / Symfony cannot find new console generated bundles - php

We're working with Sylius and trying to create new bundles. Through the console using php bin/console generate:bundle a new bundle is easily createable. however when we try to run the site we get the error: ClassNotFoundException in AppKernel.php line 36: We are registering our new bundle in the AppKernel.php file, and editing the composer.json file to autoload the new bundle but nothing seems to work. We have tried every solution mentioned on SO without luck. Can anyone point us in the right direction?
MUCH APPRECIATED-!
public function registerBundles()
{
$bundles = [
new \Sylius\Bundle\AdminBundle\SyliusAdminBundle(),
new \Sylius\Bundle\ShopBundle\SyliusShopBundle(),
new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(),
new \FOS\OAuthServerBundle\FOSOAuthServerBundle(), // Required by SyliusApiBundle
new \Sylius\Bundle\ApiBundle\SyliusApiBundle(),
new \AppBundle\AppBundle(),
//NEW BUNDLE
new TGB\AmazonBundle\AmazonBundle(),
];
return array_merge(parent::registerBundles(), $bundles);
}
from our composer.json file
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle/",
"TGB\\AmazonBundle\\": "src/TGB/AmazonBundle/"
},
"classmap": ["app/AppKernel.php", "app/AppCache.php"]
},

Found the answer, we needed to run composer dump-autoload it was caching and wouldn't go look for the new classes that it needed to load.

Related

Generating bundle with SensioGeneratorBundle gives error symfony 3.4

I'm getting trouble generating a new bundle siwth sensio/generator-bundle, symfony ^3.4, what i do is:
> symfony new calendar --version=^3.4
> cd calendar
> composer require sensio/generator-bundle
> php bin/console generate:bundle:
> planning on sharing this bundle: yes
> Bundle namespace: Nmateo/CalBundle
> Bundle name"NmateoCalBundle": [enter]
> Target Directory"src/": [enter]
> Configuration format"xml": [enter]
> symfony new calendar --version=^3.4
> cd calendar
> composer require sensio/generator-bundle
> php bin/console generate:bundle:
> planning on sharing this bundle: yes
> Bundle namespace: Nmateo/CalBundle
> Bundle name"NmateoCalBundle": [enter]
> Target Directory"src/": [enter]
> Configuration format"xml": [enter]
Then i get this message:
The command was not able to configure everything automatically.
You'll need to make the following changes manually.
-Edit the composer.json file and register the bundle
namespace in the "autoload" section:
-Edit /home/nmateo/Documents/calendar/src/Kernel.php
and add the following bundle in the AppKernel::registerBundles() method:
new Nmateo\CalBundle\NmateoCalBundle(),
So i registered it in autoloader:
"autoload": {
"psr-4": {
"App\\": "src/",
"Nmateo\\CalBundle\\": "src/Nmateo/CalBundle/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/",
"Nmateo\\CalBundle\\": "src/Nmateo/CalBundle/"
}
},
and added new Nmateo CalBundle NmateoCalBundle to the end of registerBundles in Kernel.php:
public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
new Nmateo\CalBundle\NmateoCalBundle();
}
and do the composer dump-autoload:
Generated autoload files containing 0 classes
And then i start my server and got this error, i tried many times..:
EDIT: Sorry for the text in codes quote but the stackoverflow is giving me headaches since it dont want to publish my question it says: please place your code in code quotes buy all my code was already in codes quotes..
Read GitHub repository before use any bundle. This bundle can`t works well with Symfony 4.0
Symfony 3.4 is equal to LTS Symfony 4.0
Sensiolabs uses *.4th versions as feature-freeze version.
https://github.com/sensiolabs/SensioGeneratorBundle
"WARNING: This bundle does not support Symfony 4. It also does not support the new bundle-less directory structure as created by Symfony Flex. Use the Maker bundle instead."
Symfony 3.4 has an issue with generating a new bundle its a known issue,
https://github.com/symfony/symfony-standard/issues/1098
to fix that change your code as below
"autoload": {
"psr-4": {
"": "src/"
}
What we did is that we specified in the composer that every new generated bundle will be imported from /src
To finish the procedure you'll need to run
composer dump-autoload
make sure you have the file composer.phar or download it from here https://getcomposer.org/download/
hope it helps

How to auto generate a bundle in Symfony3? [duplicate]

This question already has answers here:
Symfony3 ClassNotFoundException after bundle creation
(4 answers)
Closed 5 years ago.
I've tried to automatically generate a bundle in Symfony3, using instructions on the official site: https://symfony.com/doc/current/bundles.html, but failed.
I created a directory \src\TestBundle in my project with a file TestBundle.php in it. I extended my class TestBundle from Bundle and added the use Symfony\Component\HttpKernel\Bundle\Bundle; line before the class declaration.
I added my new bundle name to the bundles list in AppKernel.php.
Finally, when I execute $ php bin/console generate:bundle --namespace=TestBundle, I get this error:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "TestBundle" from namespace "TestBundle".
Any ideas why that happens?
If you launch this command:
php bin/console generate:bundle --namespace=TestBundle
Symfony create the Bundle for you, you don't need to create file and add It into the AppKernel file.
Try to remove all your files and reference about TestBundle and after launch only the command, Symfony will create and register the Bundle for you.
Instead i you want to create manually thos file you need to create the directory \src\TestBundle and inside It file TestBundle.php.
After you need to register It into your AppKernel.
After you need to tell to composer to load that bundle, if is not already done use this configuration for autoload inside composer.json
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
And finally launch the command inside your console:
composer dump-autoload

Load git repo with composer - autoloading issue

I have a github repository https://github.com/KoulSlou/UPS and I would like to add it to my project.
In project root I created composer.json file and defined the following autoloading properties:
{
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}
When I run
php composer.phar install
repository is being downloaded, but it looks like autoloader is not working. When I try to initialize one of the classes
$test = new Ups()
I got the following error:
Fatal error: Class 'Ups' not found in application/....
Did I define "autoload" property incorrectly?
I'd suggest not using the "files" autoloader, because that isn't very automatic - the files mentioned here are ALWAYS included. Replacing it with "classmap" would be better. And then you'd not be required to mention ALL files, but you can simply state the directory you want to have scanned for classes.
Now what I don't see anywhere: Did you initialize Composer's autoloader anywhere? This usually is something like
require "vendor/autoload.php";
Finaly, I have found out what was the problem. composer.json file in the project I was trying to load - UPS library -was invalid. I was able to download files when I ran:
composer.phar install
but it looks like composer.json file was ignored. I found it out when I ran
composer.phar update
and got
No valid composer.json was found
With option -v I got error that "name" is undefined index. So, I simply added "name" field to the composer.json. Final version is:
{
"name":"KoulSlou/UPS",
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}

Symfony 2 install third party bundle manually

My Internet conexion is useless to install a symphony 2 dependency with composer as far as I can say. Is there a way to install a third party bundle manually? I have been looking in Google and I did not find any useful thing so far. About my connection issue, I started this thread to try and find a solution to install within this connection. here I am trying to find clues to a solution to install manually.
Regards
1 Create under vendor directory the path for bundle :
/*like mycompany*/ /*like product-bundle*/ /*like MyCompany*/
vendor/yourbundlenamespace/your-bundle-name-bundle/YourBundleNameSpace/
2 Go to in the new path and put the content of budle (or clone from github).
3* #deprecated
Load the reference path on autoload, so go to in
vendor/composer/autoload_namespaces.php
and put in the array
'YourBundleNameSpace\\YourBundleNameBundle' => array($vendorDir . '/yourbundlenamespace/your-bundle-name-bundle');
4 Register bundle: go to in
app/AppKernel.php
and put in the array $bundles the new bundle:
```
public function registerBundles()
{
$bundles = array(
//...
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
//...
// this is our bundle :)
$bundles[] = new YourBundleNameSpace\YourBundleNameBundle\YourBundleNameBundle();
}
```
UPDATE
Point 3: this method is not performance because when update via composer is overwrite, instead, you should do:
-go to on app/autoload.php
and put following code after $loader definition
```
//add customs classes
$loader->add('YourBundleNameSpace\\YourBundleNameBundle','vendor//yourbundlenamespace/your-bundle-name-bundle');
```

ClassNotFoundException Symfony

I'm trying to make my website live but keep getting the same error...I've tried looking everywhere and using everyone solution with no luck.
Error
ClassNotFoundException: Attempted to load class "DestinationAppBundle" from namespace "Destination\AppBundle" in /home/dcms/public/html/dcms/apha/app/AppKernel.php line 19. Do you need to "use" it from another namespace?
AppKernel.php
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new Destination\AppBundle\DestinationAppBundle(),
new Destination\Auth\HashInterfaceBundle\DestinationHashInterfaceBundle(),
);
File Structure
src
Destination
AppBundle
Any ideas?
first: check if the file src\Destination\AppBundle\DestinationAppBundle.php does exist
second: open DestinationAppBundle.php and chek if the namespace is Destination\AppBundle
third: check if the classname declaration is the same as the filename (in your case : DestinationAppBundle)
Update composer.json like this:
"psr-4": {
"AppBundle\\": "src/AppBundle",
"UserBundle\\": "src/UserBundle"
},
After edit composer.json YOU MUST RUN composer dump-autoload
Verify in AppKernel.php the use of this class DestinationAppBundle
And the namespace in the class DestinationAppBundle
What is the namespace in your DestinationAppBundle.php ?
It should be :
<?php
namespace Destination\AppBundle;
If you are sure the namespace and service definition are right you can try:
Upgrade Symfony version. I had an strange issue with Symfony 2.8.11, new services added gave me a ClassNotFoundException. Upgrading to 2.8.17 solved it.
Try remove app/cache/* contents
Check if you are using a pre-generated boostrap cached file without the "--no-dev" option (composer dump-autoload --optimize --no-dev --classmap-authoritative) and launch command again.
Symfony uses PSR autoload.
You need to configure autoload in the composer.json file:
"autoload": {
"psr-4": {
"": "src/"
},
This will include all files and folders under the \src folder

Categories