I've extended SonataUserBundle and I'm trying to put french translations in it.
Here is my admin service definition:
sonata.admin.user:
class: Application\Sonata\UserBundle\Admin\Entity\UserAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: user, label: users }
arguments:
- null
- Application\Sonata\UserBundle\Entity\User
- SonataAdminBundle:CRUD
calls:
- [setTranslationDomain, [SonataUserBundle]]
- [setUserManager, [#fos_user.user_manager]]
- [setSecurityContext, [#security.context]]
As you can see, the translation domain is set to SonataUserBundle.
I have set some labels in src/Application/Sonata/UserBundle/Resources/translations/SonataUserBundle.fr.yml
#...
list:
label_firstname: Prénom
label_username: Nom d'utilisateur
#...
But they are not taken into account (cache cleared)
However, if I remove this file, it insults me with
The file ".../src/Application/Sonata/UserBundle/Resources/translations/SonataUserBundle.fr.yml" must contain a YAML array.
Modifying the translation domain has no effect at all.
What am I doing wrong ?
You have add label_translator_strategy: sonata.admin.label.strategy.underscore in the service definition:
sonata.admin.user:
class: Application\Sonata\UserBundle\Admin\TestAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: users, label: users, label_translator_strategy: sonata.admin.label.strategy.underscore }
arguments:
- null
- Application\Sonata\UserBundle\Entity\User
- SonataAdminBundle:CRUD
calls:
- [setTranslationDomain, [SonataUserBundle]]
- [setUserManager, [#fos_user.user_manager]]
- [setSecurityContext, [#security.context]]
I have tested in a new project, and it works fine.
Related
I was able to setup Sonata Admin with translated entities using Gedmo Doctrine Extensions:
# Doctrine Extensions Configuration
stof_doctrine_extensions:
default_locale: '%locale%'
orm:
default:
timestampable: true
blameable: true
translatable: true
# Sonata Translation Configuration
sonata_translation:
locales: [en, fr, it]
default_locale: '%locale%'
gedmo:
enabled: true
However every time I create a new entity, the translatable fields in the other languages starts empty.
English language selected:
Italian language selected:
It becomes very difficult to translate items if I don't know what they are in English.
Is there an option so that when I create an entity in English it populates also the entities in the other languages with the same content?
You should add fallback option to fields you need:
/**
* #var string
*
* #Assert\NotBlank()
*
* #Gedmo\Translatable(fallback=true)
*
* #ORM\Column(type="string", length=255)
*/
private $title;
I guess You forget to load the event listener service.
For each Gedmo extension (Timestampable, Blameable, Translatable etc. ) you have to register service in your service container.
For Translatable:
## app/config/services.yml
services:
## ...
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ #annotation_reader ] ]
- [ setDefaultLocale, [ %locale% ] ]
- [ setTranslationFallback, [ false ] ]
I begin with Symfony 4 and I want to install FosUserBundle with this link :
https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
First :
My problem is that I don't know where to find the "app/config/config.yml" file to uncomment the translator and to configure :
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
Second :
I think that I have to create the security.yml file in "config/packages/" directory, is that right ?
Third :
And in which file to add the route ?
Could you help me, please ? :)
I've resolved the problem followed this:
download FOSUserBundle using composer:
composer require friendsofsymfony/user-bundle "~2.0"
At the end of the installation you will have the following error message :
The child node "db_driver" at path "fos_user" must be configured.
Create your User class
Create src/Entity/User.php as custom user class who extend the FOSUserBundle BaseUser class.
<?php
//src/Entity/User.php
namespace App\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
Configure your application's security.yml
Modify config/packages/security.yaml to setup FOSUserBundle security
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
Configure the FOSUserBundle
Create a new file config/packages/fos_user.yaml for the configuration of FOSUserBundle
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: App\Entity\User
from_email:
address: "vincent#vfac.fr"
sender_name: "vincent#vfac.fr"
Update config/packages/framework.yaml to add templating configuration
framework:
templating:
engines: ['twig', 'php']
Import FOSUserBundle routing
Create config/routes/fos_user.yaml
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
Update your database schema
If not already done, you must create your database
php bin/console doctrine:database:create
Update the schema with the informations from your User class entity
php bin/console doctrine:schema:update --force
At this point, all is installed and configured to use FOSUserBundle in Symfony 4. Run the following command to check if all is ok
composer update
If you don't have any error message, you can test !
You can run the web server to test your application
php bin/console server:start
all tutorial here:
https://vfac.fr/blog/how-install-fosuserbundle-with-symfony-4
This is the solution I found to work.
First:
app/config/config.yml doesn't exist anymore instead the configs have been moved to the config folder. For the FOS User Bundle the correct location: /config/packages/fos_user.yaml. As already noted, use the dev-master version FOSUserBundle, it supports 4(still a little work in progress but good enough).
Second:
You are correct, a simple solution is do a composer require security and the recipe will take care of that for you.
https://symfony.com/doc/current/security.html for more info.
Third:
The default FOS User Bundle routes:
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
More info on the FOS routing (step 6)is helpful
https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
Also, I recommend looking at the yaml samples in symfony routing documentation. It may make things a little more clear when configuring the routes with relation to FOS User Bundle.
https://symfony.com/doc/current/routing.html
ok i have same problem and it should be like that
first:as #sean Baharmi says you should create /config/packages/fos_user.yaml and inter configuration like this
fos_user:
db_driver: orm
firewall_name: main
user_class: App\Entity\Users
from_email:
address: "hello#youmail.com"
sender_name: "Sender Name"
then in framework.yaml you should enter following because of FOSUserBundle dependencies
templating:
engines: ['twig', 'php']
also for add routing in /config/rourtes/routes.yaml add
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
then it is ready to work
hope works for you
You can't use FOSUSerBundle on Symfony4 at the moment. Support has not been merged yet. You can follow development here.
If you want to use FOSUserBundle with Symfony4 you can try the patch Ryan provided here.
I trying to install SonataAdmin on my Symfony Project but at the end of the part-2 of the documention when i'm trying to go on "http://localhost:8000/admin/" I have a error : "You have requested a non-existent service "admin.category" in . (which is being imported from "C:\wamp64\www\Sonata/app/config\routing.yml"). Make sure there is a loader supporting the "sonata_admin" type."
I have no idea why, i give give my all my parameters code maybe it's can help you to understand my problem.
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
`
The indentation is going wrong i add you a picture of this file. Service code
The sonata admin services must be public. In your config, you have default as public: false and that is why you get this error.
So you have 2 options:
Specify public: true for your admin service (in your example file)
Or the better way: create a new services file (eg admin.yml) where you dont use these defaults (the _defaults key with public: false). Public is true by default, so you don't have to specify that by _defaults. In this case you must import your new file in config.yml to work:
Top of app/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: admin.yml }
app/admin.yml content:
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
I think you did a mistake by writing your category.admin service in: Sonata/app/config/routing.yml,
instead of Sonata/src/YourAdminBundle/Resources/config/services.yml
Run this command on terminal. Because You might have missed installing
php composer.phar require sonata-project/doctrine-orm-admin-bundle
After This Add this code below to your AppKernel.php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
// set up basic sonata requirements
// ...
new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
// ...
);
}
It throws this error:
Unrecognized option "handler" under "fos_user.registration.form"
And the files are:
//services.yml
app.form.registration:
class: AppBundle\Form\AccomodationFrontSignUpType
tags:
- { name: form.type, alias: app_user_registration }
app.form.handler.registration:
class: AppBundle\Form\Handler\RegistrationFormHandler
arguments: ["#fos_user.registration.form", "#request", "#fos_user.user_manager", "#fos_user.mailer", "#fos_user.util.token_generator"]
scope: request
public: false
//config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
group:
group_class: AppBundle\Entity\Group
service:
mailer: swiftmailer.mailer.strato
from_email:
address: webmyhomepage#strato.com
sender_name: myHomepage
registration:
form:
handler: app.form.handler.registration
type: app_user_registration
confirmation:
template: FOSUserBundle:Registration:email.txt.twig
enabled: true
Any help is welcome, thanks
The error means that there is an invalid parameter in your config. The key handler in your config
-> handler: app.form.handler.registration
if you take a look inside the fos user bundle DependencyInjection folder, Configuration.php file, that option does not exist. so its invalid :).
There you can see witch options are available. In my current version of this bundle i have this options available under form: type, name, validation_groups.
Also you can see the default configuration for a bundle with:
php app/console debug:config FOSUserBundle
I want to set Amazon S3 settings from database and not from parameters.yml
can someone point me to right direction on how to use database (doctrine) parameters before symfony2 calls services.
#app/config/config.yml
services:
acme.aws_s3.client:
class: Aws\S3\S3Client
factory_class: Aws\S3\S3Client
factory_method: 'factory'
arguments:
-
key: %amazon_s3.key%
secret: %amazon_s3.secret%
# knp_gaufrette
knp_gaufrette:
adapters:
profile_photos:
aws_s3:
service_id: 'acme.aws_s3.client'
bucket_name: 'myBucket'
options:
directory: 'myDirectory'
acl: 'public-read'
Use your own service factory for acme.aws_s3.client service configuration.
services:
acme.aws_s3.client:
class: Aws\S3\S3Client
factory_class: My\S3ClientFactory
factory_method: createClient
arguments: [ #settings_repository ]
#settings_repository - any service that has access to db. E.g. doctrine entity repositry, or entire object manager.
My\S3ClientFactory::createClient - pretty much the same as native Aws\S3\S3Client::factory except that it would take params from db.
Hope this helpful.