Symfony 4.2 documentation :
"
For example, to override the translations defined in the Resources/translations/FOSUserBundle.es.yml file of the FOSUserBundle, create a<your-project>/translations/FOSUserBundle.es.yml file.
"
That's doesn't work for me and i clean my cache ! Any one have some idea ?
Symfony take always the FOSUserBundle.es.yml of vendor.
Create a folder in /translations/FOSUserBundle/, move the translations you need and redo it.
"friendsofsymfony/user-bundle": "^2.1"
"symfony": "require": "4.2.*"
Related
I have a number of private, custom bundles that I use in my Symfony projects. Under Symfony 3, they lived in a sub-directory of src:
src/
DSL/
DSLLibraryBundle/
DSLTelnetBundle/
...
SiteBundle/ # (or AppBundle)
Under Symfony 4, the application-specific bundle is gone and it's unclear to me where my custom bundles should live.
The documentation on bundles (https://symfony.com/doc/current/bundles/best_practices.html#bundles-naming-conventions) provide no specific recommendations for placing custom bundles.
I have tried placing my DSL directory directly under the project directory and under src/. I end up with undefined class errors either way.
I currently have:
src/
DSL/
LibraryBundle/
DSLLibraryBundle.php
The bundle file:
// src/DSL/DSLLibrary/DSLLibraryBundle.php:
namespace DSL\LibraryBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class DSLLibraryBundle extends Bundle
{
}
The entry in bundles.php:
DSL\LibraryBundle\DSLLibraryBundle::class => ['all' => true],
Current error when running a console command:
PHP Fatal error: Uncaught
Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to
load class "DSLLibraryBundle" from namespace "DSL\LibraryBundle".
A couple of notes:
- My custom bundles are not installed via Composer
- The actual DSL/ directory will be a symlink once I get this working
Update April 12, 2019:
In the end, I took a completely different approach than my initial attempts.
In a nutshell, I now use composer to include my custom bundles.
My custom bundles live in their own directory tree.
Each bundle must have a valid composer.json file defining the bundle. For example:
{
"name": "dsl/base-bundle",
"description": "Base bundle required by all other DSL bundles",
"type": "symfony-bundle",
"version": "2.1.0",
"license": "proprietary",
"authors": [{"name": "David M. Patterson", "email": "dpatterson#example.com"}],
"minimum-stability": "stable",
"require": {
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Dsl\\BaseBundle\\": "src/"
}
}
}
Then define a custom repository in the project's composer.json file:
"repositories":[
{
"type": "path",
"url": "/full/path/to/DslBaseBundle"
},
], ...
Then do a composer require dsl/base-bundle
Composer will create a symlink in vendor/ to the bundle and everything works as expected from there on.
My personal library is a regular Symfony project with a lib sub-directory that contains my bundles, each in its own sub-directory below lib/.
The Symfony application provides me with a convenient test bed. Note that the custom bundles must be included in it the same as for any other Symfony project.
#Stnaire, hope that helps.
It's actually not as bad as it's painted in other comments - you can still have your private bundles inside src/, you just have to explicitly exclude them from autowiring, so they don't get accidentally loaded with an incorrect namespace.
Lets say you have a PrivateBundle in src/PrivateBundle.
You set up it's autoloading in composer.json like so:
"autoload": {
"psr-4": {
"App\\": "src/",
"SomeNamespace\\PrivateBundle\\": "src/PrivateBundle/"
}
}
and in your services configuration (I usually use config/services.yaml) do this:
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/PrivateBundle'
If you don't add this exclude, your SomeNamespace\PrivateBundle\* classes get automatically loaded by Symfony as App\PrivateBundle\*, but contain namespace SomeNamespace\PrivateBundle;, so when PHP detects a usage of SomeNamespace\PrivateBundle it autoloads them again through Composer, resulting in Cannot declare class *, because the name is already in use errors.
Update 30-Jan-2017:
Okay. As far as I can tell, Symfony 4 is, effectively, private bundle hostile.
Additional work just kept turning up more and more problems (like getting unit testing to work for a private bundle).
I am currently pursuing other options that won't result in too much additional daily work.
Please ignore my original answer below.
--
My original answer:
After some more digging I realized that the classes in my custom bundle directory tree were not being picked up by composer during dump-autoload.
I think this is because Symfony 4 is not expecting any bundles except in vendor/.
The solution was to add my library directory to composer.json.
So My project tree now contains a directory for my private, custom bundles.
<projectName>/
assets/
...
DSL/
DSLLibraryBundle/
DSLTelnetBundle/
...
public/
src/
...
My composer.json autoload.psr-4 entry now looks like this:
"autoload": {
"psr-4": {
"App\\": "src/",
"DSL\\": "DSL/"
}
},
Symfony4 no longer uses bundles inside the src/ . Bundles are only used within vendor/ as dependencies.
After running composer update , I keep having the error below:
Warning: Ambiguous class resolution,
"Doctrine\ORM\Persisters\Entity\BasicEntityPersister" was found in
both "$baseDir .
'/engine/Library/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php"
and
"/var/www/html/shop5/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php",
the first will be used. Warning: Ambiguous class resolution,
"Doctrine\Common\Proxy\AbstractProxyFactory" was found in both
"$baseDir .
'/engine/Library/Doctrine/Common/Proxy/AbstractProxyFactory.php" and
"/var/www/html/shop5/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php",
the first will be used.
I have tried to run the following commands but none of them works:
composer dump-autoload -o
composer clearcache
Any idea how to fix this issue ?
Thank you
[shopware5 - php7.0]
This is the normal behavior of Shopware.
The Doctrine library uses the final class statement quite often and in order to get it to work with the Shopware attribute system the classes were replaced through the composer autoloading. You can find the changed files in shopware/engine/Library/Doctrine/Common
FYI: This is the reason why Shopware only works when the composer autoloading is optimized.
composer dump-autoload --optimize
Otherwiese you will encounter random errors from invalid or wrong entities.
To get rid of these warning you should add files with ambiguous classes to exclude-from-classmap in your composer.json:
"autoload": {
...
"exclude-from-classmap": [
...
"vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php",
"vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php"
]
},
Then dump-autoload will ignore these files.
This is the reason why Shopware only works when the composer autoloading is optimized.
I didn't investigate how this is done in Shopware, but this also could be fixed/improved. For composer more precise definitions for namespaces have a precedence. So if you have this in your autoload:
"autoload": {
"psr-0": {
"somevendor\\somepackage\\": "vendor/somevendor/somepackage/",
"somevendor\\somepackage\\db\\": "overrides/somevendor/somepackage/db/"
}
},
And if you request for somevendor\somepackage\db\Entity class, the composer will first search in overrides/somevendor/somepackage/db/Entity.php and only if it can't find it, it will try vendor/somevendor/somepackage/db/Entity.php. This is because definition for somevendor\somepackage\db namespace is more precise than for somevendor\somepackage.
So if you want to override 3rd-party classes in this way, you should define more precise namespaces than 3rd-party library do.
I have created a bundle with symfony 2.3 but in this case (cause my teacher asked to me) outside src/ folder so I have ../symfony/fuentes/NameBundle instead of ../symfony/src/NameBundle. The new line appears in AppKernel and my new bundle appears on routing.yml, but when I try to launch the server
Bundle generation
Generating the bundle code: OK
Checking that the bundle is autoloaded: FAILED
Confirm automatic update of your Kernel [yes]?
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]?
Importing the bundle routing resource: OK
The command was not able to configure everything automatically.
You must do the following changes manually.
- Edit the composer.json file and register the bundle
namespace in the "autoload" section:
I have edit autoload and tried a lot of things (looking for here) but it appears the same error always.
C:\Users\Akenateb\Documents\UOC\AULAMENTOR\Symfony>php app/console server:run 127.0.0.1:8080
PHP Fatal error: Class 'AulaMentor\ExdosBundle\AulaMentorExdosBundle' not found in C:\Users\Akenateb\Documents\UOC\AULAMENTOR\Symfony\app\AppKernel.php on line 20
Can someone help me? I'm really stuck with it.
Thanks in advance.
First of all I want to thanks to the people who has answered. Here is what we have to do if we want to create a bundle outside 'src' folder, for example in 'fuentes' > '..Symfony/fuentes'.
If you have create with 'generate:bundle' I suggest you accept when the generator ask you if you want to create the complete structure, if you have create a bundle with generator goto step 3.
1- Make sure you have registered bundle in AppKernel and It exists there a line like this:
new YourProject\NameprojectBundle\YourProjectNameprojectBundle(),
2- Make sure you have adding a route to your app/config/routing and 'routing.yml' has you new bundle route, like this (you can add a prefix to your url, in this case fuentes):
your_project_name:
resource: "#YourProjectNameprojectBundle/Resources/config/routing.yml"
prefix: /fuentes
3- We edit 'app/autoload.php' and we add this line:
$loader->add('YourProject',realpath(__DIR__.'/../fuentes'));
Finally we can update assets doing with command line:
php app/console assets:install web
Hope it helps to someone.
Best Regards.
Do exactly as said in the comment:
Edit the composer.json file and register the bundle namespace in the "autoload" section
The src folder is automatically loaded using PSR convention. If you set classes outside of the src folder, they have to be declared as well.
In your composer.json file you can add another element in the autoload section. Here's an example that I think will work for your use case:
"autoload": {
"psr-0": {
"NameBundle\\": "fuentes/",
"": "src/"
}
},
Here's how to add simply another bundle outside your Symfony project, when we don't want to mess up with composer.json file.
The 2 first points are the same as in the Michael J.'s answer. Now to the 3rd point:
Say, we need to add a OurCompany/SomeBundle residing in the other project, which relative path is ../../OtherProject/src/OurCompany/SomeBundle to CurrentProject/app dir.
So we add this bundle to the CurrentProject application this way:
$loader->add('OurCompany\\SomeBundle', realpath(__DIR__.'/../../OtherProject/src'));
PLEASE NOTICE how the slashes and backslashes should be used (the remaining / or \ at the end doesn't matter, it's smart enough to figure it out).
And for the whole namespace to be loaded (all bundles namespaces in the other project available in CurrentProject):
$loader->add('OurCompany', realpath(__DIR__.'/../../OtherProject/src'));
I need to generate Excel files, I did a search and phpexcel seems to be good and stable. I'd like to know:
* how to integrate it into my project given that it's a symfony2.0 project
* if I can do all what I do normally on excel file through this php library (cells colors, adding lists ...)
Thanks,
If you are using composer, and since PHPExcel is registered with Packagist, then this is simply a matter of adding PHPExcel to your composer.json config, for example:
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4",
"doctrine/orm": "~2.2,>=2.2.3",
...
"phpoffice/phpexcel": "~1.8.0",
This is from a Symfony 2.4 configuration, but should work equally well for any version of Symfony.
Run $ php composer.phar update to grab the package and it should then be autoloaded into your project. You can then use PHPExcel immediately, say in a controller:
<?php
namespace SomeProject\SomeBundle\Controller;
use PHPExcel;
Or just reference \PHPExcel directly from within your code.
Update:
Note that composer found it's way into Symfony from around version 2.0.4, and was used as an alternative to the old deps and deps.lock files until the deps approach was deprecated from 2.1.
If you're still using deps, you can append this to your deps file:
[PHPExcel]
git=git://github.com/PHPOffice/PHPExcel.git
target=phpexcel
and run php bin/vendors install. This will put the PHPExcel in vendors/phpexcel.
Register PHPExcel with the registerPrefixes array in app/autoload.php and it should be available to your project as described above.
$loader->registerPrefixes(array(
'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
'Twig_' => __DIR__.'/../vendor/twig/lib',
'PHPExcel' => __DIR__.'/../vendor/phpexcel/Classes'
));
LiipImagineBundle doesn't seem to have instructions for installing itself using composer (github page) so I added
"liip/imagine-bundle": "*",
to my composer.json and updated. Everything went fine until I tried to register the bundle in appKernel.php with the line
new Liip\ImagineBundle\LiipImagineBundle(),
Php gives the error
Fatal error: Class 'Liip\ImagineBundle\LiipImagineBundle' not found in C:\xampp\htdocs\xxxx\Symfony\app\AppKernel.php on line 24
As far as I can see LiipImagineBundle is in the right place in the vendors folder. Anyone have any idea where I'm going wrong? Thanks in advance.
*Edit I should add I'm using symfony 2.1
I had to change the line in composer to this:
"liip/imagine-bundle": "*#dev"
notice the #dev at the end. This tell composer that I am willing to accept "dev" stability
I contacted the developer of the bundle and found that there is a vendor/composer/autoload_namespaces.php where you can manually add entries and the bundle wasn't there.
This guide was helpful: Composer Namespaces in 5 Minutes
I'm using Symfony 2.1.4-DEV and including "liip/imagine-bundle": "*" on the composer.json and updating it works
Did you registered Namespace in autoload.php and registered Bundle in AppKernel.php? If not the class will be not avialable to use until registration there.
As the documentation says:
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'Imagine' => __DIR__.'/../vendor/imagine/lib',
'Liip' => __DIR__.'/../vendor/bundles',
));
And
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Liip\ImagineBundle\LiipImagineBundle(),
);
}
Update
Run composer update it will regenerate your bootstrap files. tell me if any luck with this.