Generating bundle with SensioGeneratorBundle gives error symfony 3.4 - php

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

Related

Changing vendor directory can't find files anymore using Symfony 3.4

I'm trying to change the directory of my dependencies on a Symfony 3.4 application.
I need that because I'm working on macOS with Docker and I'd rather have them not shared with the host since the file synchronization is too slow.
The related documentation, says:
The change in the composer.json will look like this:
{
"config": {
"bin-dir": "bin",
"vendor-dir": "/some/dir/vendor"
},
}
That I did
Then, update the path to the autoload.php file in app/autoload.php:
// app/autoload.php
// ...
$loader = require '/some/dir/vendor/autoload.php';
I don't have any autoload.php file in my app directory.
Am I missing something in the doc ?
The application generates the following fatal error:
Warning: require(/some/dir/vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php): failed to open stream: No such file or directory in /vendor/composer/autoload_real.php on line 66
Fatal error: require(): Failed opening required '/some/dir/vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php' (include_path='.:/usr/local/lib/php') in /vendor/composer/autoload_real.php on line 66
I originally created the application with:
$ composer create-project symfony/framework-standard-edition test "3.*"
Open your composer.json file in editor.
Look for "autoload-dev" section
Remove whole "files" part (if exist)
Save file
Run composer install once again
Enjoy the party.
Sample code:
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
},
"files": [
"vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
},
In Symfony 3.4, the file app/autoload.php is removed so you should:
replace old vendor path by the new vendor path directly in web/app.php, web/app_dev.php, bin/console and var/SymfonyRequirements.php files
Rerun the command $ composer install
I had the same issue and it was resolved doing the next.
Follow these 3 steps
1. First of all, modify composer.json to use the new vendor path:
"config": {
...,
"vendor-dir": "/app-vendor"
},
And remove the next line:
"files": ["vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"]
2. Secondly, if you are using docker-compose add a new volume where you'll put your vendors.
volumes:
...
- /app-vendor
PD: /app-vendor is a mounted volume which is now empty directory.
3. Lastly, write require '/app-vendor/autoload.php'; to:
my_project_name/bin/console
my_project_name/web/app.php
my_project_name/web/app_dev.php
PD1: Simply, this line is pointing to the new vendor path.
PD2: It's not necessary to modify any other file (like var/SymfonyRequirements.php as I could read).
Check your changes
Once the changes are ready, remove vendor/ and also remove the containers to avoid future problems.
Start your new containers and execute composer install. Now, /vendor will be /app-vendor, it won't be in the root folder of the project anymore.
For more details, I'd recommend you to go to my docker-symfony repository and check the commits. You'll see a benchmark progression and another tips like cached volumes and non-shared /cache && /logs folders.
All for Symfony 3.4.

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

Sylius / Symfony cannot find new console generated bundles

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.

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"
]
}
}

Unable to use Propel 2 (Class not found)

I'm tryng from several days to setup and use Propel now 2.0. PHP version is 5.4.4-14+deb7u5
What I have done:
0) Fresh LAMP with a folder "test" in /var/www
1) Composer.json with
{
"require": {
"propel/propel": "2.0.*#dev"
}
}
(also tried with the alpha indicated in home page, no success, download but i cannot use)
2) It download all necessary files.
3) I can launch "vendor/bin/propel" and it exit after some green text.
4) I create the schema.xml with foreign keys indicated in http://propelorm.org/documentation/02-buildtime.html
5) I set up buildtime.cconfiguration
6) I can create the sql:build and the model:build (I find the bookstore.sql in generated-sql and the classes in generated-classes)
7) I CANNOT insert the sql. I launch sql:insert, no error at screen but no insert in database (connection/password is okay, double checked).
8) I load myself SQL in database.
9) I create an index.php with this:
<?php
// setup the autoloading
require_once 'vendor/autoload.php';
use Propel\Runtime\Propel;
use Propel\Runtime\Connection\ConnectionManagerSingle;
$serviceContainer = Propel::getServiceContainer();
$serviceContainer->setAdapterClass('bookstore', 'mysql');
$manager = new ConnectionManagerSingle();
$manager->setConfiguration(array (
'dsn' => 'mysql:host=localhost;dbname=my_db_name',
'user' => 'my_db_user',
'password' => 's3cr3t',
));
$serviceContainer->setConnectionManager('bookstore', $manager);
echo 'All ok, for now...';
$author = new Author();
$author->setFirstName('Jane');
$author->setLastName('Austen');
$author->save();
/* /end of php file */
The echo is printed normally but next row script exit with error 500 and in Apache log I read "Class author not found".
Is there some other config to adjust other than indicate in the guide?
I resolved a similar situation by adding this to my composer.json and then running install again.
"autoload": {
"classmap": ["generated-classes/"]
}
I had this error too. Apparently the problem was with the autoload configuration and running a php composer.phar dump-autoload command fixed it.
php composer.phar dump-autoload
If you want to solve this problem you should combine jerrygarcuih's and Abaobi Orajiaku's awnsers.
Thank you guys.
Add the models folder to the composer.json
"autoload": {
"classmap": ["generated-classes/"]
}
Then run composer 'dump-autoload'.
All generated classes should be in the same namespace.
I had a similiar issue. And i solved it by including the exact path to the classmaps.
"autoload": {
"classmap": [
"path/to/generated-classes/",
"path/to/generated-classes/Base/",
"path/to/generated-classes/Map/"
]
}

Categories