Notice: Undefined index: translation_domain - symfony

I apologize for my English.
Some time ago I encountered a problem when developing my project on the Symfony. The problem arose in SonataAdminBundle.
The error looks like this
My Admin class
<?php
namespace Flatbel\FlatBundle\Admin;
use Flatbel\FlatBundle\Entity\User;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
class AdminAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Основная информация', array('class' => 'col-md-8'))
->remove('userid')
->add('flattype', 'choice', array(
'choices' => array(
'VIP' => 'VIP',
'Стандарт' => 'Стандарт',
'Бюджет' => 'Бюджет'
),
'choices_as_values' => true, 'label' => 'Тип квартиры', 'placeholder'=>'Выбрать...'
))
->add('numberofbeds','choice', array(
'choices' => array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'6+' => '7',
),
'choices_as_values' => true, 'label'=>'Количество спальных мест', 'placeholder'=>'Выбрать...'
))
->add('rooms','choice', array(
'choices' => array(
'1' => '1',
'2' => '2',
'3' => '3',
'4+' => '4',
),
'choices_as_values' => true, 'label'=>'Число комнат', 'placeholder'=>'Выбрать...'
))
->add('streettype', 'choice', array(
'choices' => array(
'Проспект' => 'Проспект',
'Улица'=>'Улица',
'Переулок'=>'Переулок'
),
'choices_as_values' => true, 'label'=>'Тип', 'placeholder'=>'Выбрать...'
))
->add('street', 'text', array('label'=>'Название'))
->add('home',null,array('label'=>'Номер дома'))
->add('priceday',null,array('label'=>'Цена за день'))
->add('pricehour',null,array('label'=>'Цена за час'))
->add('pricenight',null,array('label'=>'Цена за ночь'))
->add('floorhome',null,array('label'=>'Число этажей в дома'))
->add('floor',null,array('label'=>'Этаж'))
->add('metro','choice', array(
'choices' => array(
'Каменная горка'=>'Каменная горка',
'Кунцевщина'=>'Кунцевщина',
),
'choices_as_values' => true, 'label'=>'Ближайшее метро', 'placeholder'=>'Выбрать...'
))
->add('telnumber',null,array('label'=>'Номер телефона'))
->add('about',null,array('label'=>'Описание'))
->end()
->with('Дополнительная информация',array('class'=>'col-md-4'))
->add('tv',null,array('label'=>'Телевизор'))
->add('wifi',null,array('label'=>'Wi-Fi'))
->add('parking',null,array('label'=>'Стоянка'))
->add('microwave',null,array('label'=>'Микроволновка'))
->add('washer',null,array('label'=>'Стиральная Машина'))
->add('bath',null,array('label'=>'Ванна'))
->add('shower',null,array('label'=>'Душ'))
->add('fridge',null,array('label'=>'Холодильник'))
->add('dishes',null,array('label'=>'Посуда'))
->add('linens',null,array('label'=>'Постельное бельё'))
->remove('payornot')
->remove('description')
->end()
->with('Фотографии',array('class'=>'col-md-8'))
->add('mainphoto', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'flatphotos',
))
->add('photo1','sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'flatphotos',
))
->add('photo2','sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'flatphotos',
))
->add('photo3','sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'flatphotos',
))
->end()
;
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->addIdentifier('userid')
->addIdentifier('payornot')
->addIdentifier('flattype')
->addIdentifier('numberofbeds')
->addIdentifier('rooms')
->addIdentifier('street')
->addIdentifier('streettype')
->addIdentifier('home')
->addIdentifier('priceday')
->addIdentifier('pricehour')
->addIdentifier('pricenight')
->addIdentifier('floorhome')
->addIdentifier('floor')
->addIdentifier('tv')
->addIdentifier('wifi')
->addIdentifier('parking')
->addIdentifier('microwave')
->addIdentifier('washer')
->addIdentifier('bath')
->addIdentifier('shower')
->addIdentifier('fridge')
->addIdentifier('dishes')
->addIdentifier('linens')
;
}
public function toString($object)
{
return $object instanceof User
? $object->getUsername()
: 'Flat'; // shown in the breadcrumb on the create view
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('id')
->add('userid')
->add('payornot')
->add('flattype')
->add('numberofbeds')
->add('rooms')
->add('street')
->add('streettype')
->add('home')
->add('priceday')
->add('pricehour')
->add('pricenight')
->add('floorhome')
->add('floor')
->add('tv')
->add('wifi')
->add('parking')
->add('microwave')
->add('washer')
->add('bath')
->add('shower')
->add('fridge')
->add('dishes')
->add('linens')
;
}
}
My config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#FlatbelFlatBundle/Resources/config/config.yml"}
parameters:
locale: ru
photo_directory: 'uploads/photos/'
framework:
translator: { fallbacks: ["%locale%"] }
default_locale: ru
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form:
resources:
- 'FlatbelFlatBundle:Form:media_widgets.html.twig'
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
options:
1002: 'SET NAMES UTF8'
types:
json: Sonata\Doctrine\Types\JsonType
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
ApplicationSonataMediaBundle: ~
SonataMediaBundle: ~
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
#FOSUserBundle Configuration
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: Flatbel\FlatBundle\Entity\User
use_listener: true
use_flash_notifications: true
use_authentication_listener: true
use_username_form_type: true
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
profile:
form:
type: fos_user_profile # or 'fos_user_profile' on Symfony < 2.8
name: fos_user_profile_form
validation_groups: [Profile, Default]
change_password:
form:
type: fos_user_change_password # or 'fos_user_change_password' on Symfony < 2.8
name: fos_user_change_password_form
validation_groups: [ChangePassword, Default]
registration:
confirmation:
from_email: # Use this node only if you don't want the global email address for the confirmation email
address: registration#flatbel.by
sender_name: Registration
enabled: true # change to true for required email confirmation
template: '#FOSUser/Registration/email.txt.twig'
form:
type: fos_user_registration # or 'fos_user_registration' on Symfony < 2.8
name: fos_user_registration_form
validation_groups: [Registration, Default]
resetting:
token_ttl: 86400
email:
from_email: # Use this node only if you don't want the global email address for the resetting email
address: resetting#flatbel.by
sender_name: Resetting
template: email/password_resetting.email.twig
form:
type: fos_user_resetting # or 'fos_user_resetting' on Symfony < 2.8
name: fos_user_resetting_form
validation_groups: [ResetPassword, Default]
service:
mailer: fos_user.mailer.default
email_canonicalizer: fos_user.util.canonicalizer.default
username_canonicalizer: fos_user.util.canonicalizer.default
token_generator: fos_user.util.token_generator.default
user_manager: fos_user.user_manager.default
#SonataAdminBundle
sonata_block:
default_contexts: [cms]
blocks:
# enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
# ...
sonata_admin:
security:
handler: sonata.admin.security.handler.role
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [FlatbelFlatBundle]
filters:
cssrewrite: ~
assets:
bootstrap_js:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/dist/js/bootstrap.js"
bootstrap_css:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/dist/css/bootstrap.css"
- "%kernel.root_dir%/../vendor/twitter/bootstrap/dist/css/bootstrap-theme.css"
filters: [cssrewrite]
bootstrap_glyphicons_ttf:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf"
output: "fonts/glyphicons-halflings-regular.ttf"
bootstrap_glyphicons_eot:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/dist/fonts/glyphicons-halflings-regular.eot"
output: "fonts/glyphicons-halflings-regular.eot"
bootstrap_glyphicons_svg:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/dist/fonts/glyphicons-halflings-regular.svg"
output: "fonts/glyphicons-halflings-regular.svg"
bootstrap_glyphicons_woff:
inputs:
- "%kernel.root_dir%/../vendor/twitter/bootstrap/dist/fonts/glyphicons-halflings-regular.woff"
output: "fonts/glyphicons-halflings-regular.woff"
jquery:
inputs:
- "%kernel.root_dir%/../vendor/components/jquery/jquery.js"
sonata_media:
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
default_context: default # you need to set a context
contexts:
default: # the default context is mandatory
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
- sonata.media.provider.file
- sonata.media.provider.vimeo
formats:
small: { width: 100 , quality: 70}
big: { width: 500 , quality: 70}
flatphotos:
providers:
- sonata.media.provider.image
formats:
big: { width: 500 , quality: 70}
cdn:
server:
path: /uploads/media # http://media.sonata-project.org/
filesystem:
local:
directory: "%kernel.root_dir%/../web/uploads/media"
create: false
Please, help me. I've been trying to find my own mistake for the second week, I've been looking for similar problems on the Internet - and I do not find anything.
EDIT
My services.yml
# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/service_container.html
parameters:
# parameter_name: value
services:
FlatbelFlatFileUploader:
class: Flatbel\FlatBundle\Service\FileUploader
arguments:
$targetDir: '%photo_directory%'
admin.user:
class: Flatbel\FlatBundle\Admin\UserAdmin
arguments: [~, Flatbel\FlatBundle\Entity\User,~]
tags:
- { name: sonata.admin, manager_type: orm, label: User }
admin.flat:
class: Flatbel\FlatBundle\Admin\FlatAdmin
arguments: [~, Flatbel\FlatBundle\Entity\Flat,~]
tags:
- { name: sonata.admin, manager_type: orm, label: Flat }
admin.admin:
class: Flatbel\FlatBundle\Admin\AdminAdmin
arguments: [~, Flatbel\FlatBundle\Entity\Flat,~]
tags:
- { name: sonata.admin, manager_type: orm, label: Admin}

Your admin class extends AbstractAdmin and has no any added fields in it's form mapper. So, you don't need this: ->remove('userid'). I tried to type in my admin class the same thing and got "Notice: Undefined index: translation_domain" too.

Related

I can't create a JWT endpoint

I want to add an endpoint to SwaggerUI to retrieve a JWT token. I did everything as they say in the API platform documentation here.
I created a JwtDecorator.php file:
<?php
// api/src/OpenApi/JwtDecorator.php
namespace App\OpenApi;
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\OpenApi\OpenApi;
use ApiPlatform\OpenApi\Model;
final class JwtDecorator implements OpenApiFactoryInterface
{
public function __construct(
private OpenApiFactoryInterface $decorated
) {}
public function __invoke(array $context = []): OpenApi
{
$openApi = ($this->decorated)($context);
$schemas = $openApi->getComponents()->getSchemas();
$schemas['Token'] = new \ArrayObject([
'type' => 'object',
'properties' => [
'token' => [
'type' => 'string',
'readOnly' => true,
],
],
]);
$schemas['Credentials'] = new \ArrayObject([
'type' => 'object',
'properties' => [
'email' => [
'type' => 'string',
'example' => 'johndoe#example.com',
],
'password' => [
'type' => 'string',
'example' => 'apassword',
],
],
]);
$schemas = $openApi->getComponents()->getSecuritySchemes() ?? [];
$schemas['JWT'] = new \ArrayObject([
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'JWT',
]);
$pathItem = new Model\PathItem(
ref: 'JWT Token',
post: new Model\Operation(
operationId: 'postCredentialsItem',
tags: ['Token'],
responses: [
'200' => [
'description' => 'Get JWT token',
'content' => [
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/Token',
],
],
],
],
],
summary: 'Get JWT token to login.',
requestBody: new Model\RequestBody(
description: 'Generate new JWT Token',
content: new \ArrayObject([
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/Credentials',
],
],
]),
),
security: [],
),
);
$openApi->getPaths()->addPath('/authentication_token', $pathItem);
return $openApi;
}
}
My security.yaml
security:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
App\Entity\User: 'auto'
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
login:
pattern: ^/api/login
stateless: true
json_login:
check_path: /api/login_check # or api_login_check as defined in config/routes.yaml
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/api/
stateless: true
provider: app_user_provider
jwt: ~
main:
stateless: true
lazy: true
provider: app_user_provider
json_login:
check_path: /authentication_token
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
jwt: ~
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: /api/docs, roles: PUBLIC_ACCESS } # Allows accessing the Swagger UI
- { path: ^/authentication_token, roles: PUBLIC_ACCESS }
# - { path: ^/, roles: IS_AUTHENTICATED_FULLY }
when#test:
security:
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
algorithm: auto
cost: 4
time_cost: 3
memory_cost: 10
My service.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
App\OpenApi\JwtDecorator:
decorates: 'api_platform.openapi.factory'
arguments: ['#.inner']
App\State\UserProcessor:
tags: ['api_platform.state_processor']
bind:
$decorated: '#api_platform.doctrine.orm.state.persist_processor'
arguments:
$passwordHasher: '#Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface'
ApiPlatform\State\ProcessorInterface: ~
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
But I don't see "/authentication_token" endpoint in my swagger interface
stack:
Symfony : 6.1
PHP : 8.1
api platform: 3.0
You are missing two config files:
config/api_platform.yaml:
api_platform:
swagger:
api_keys:
JWT:
name: Authorization
type: header
config/routes.yaml:
authenticate:
path: /authentication_token
methods: ['POST']

Edit Sonata User data

I'm trying to override Sonata User Admin class, on my bundle. For the moment, I just override the configureListFields method:
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
class UserAdmin extends BaseUserAdmin
{
/**
* #param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper):void
{
$listMapper
->addIdentifier('username')
->add('email')
->add('groups')
->add('enabled', null, ['editable' => true])
->add('accountType')
->add('createdAt')
;
if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
$listMapper
->add('impersonating', 'string', ['template' => '#SonataUser/Admin/Field/impersonating.html.twig'])
;
}
}
}
I refresh my page and I got the list of users without problem. But when I click into a user to edit it, I have this error: Call to a member function getClass() on null on these lines:
$now = new \DateTime();
$genderOptions = [
'choices' => \call_user_func([$this->getUserManager()->getClass(), 'getGenderList']),
'required' => true,
'translation_domain' => $this->getTranslationDomain(),
];
// NEXT_MAJOR: Remove this when dropping support for SF 2.8
if (method_exists(FormTypeInterface::class, 'setDefaultOptions')) {
$genderOptions['choices_as_values'] = true;
}
Sonata_admin.yaml file:
sonata_admin:
title: 'Staff Admin Panel'
templates:
dashboard: '#SonataAdmin/Core/dashboard.html.twig'
security:
handler: sonata.admin.security.handler.role
role_admin: ROLE_ADMIN
role_super_admin: ROLE_SUPER_ADMIN
# information:
# GUEST: [VIEW, LIST]
# STAFF: [EDIT, LIST, CREATE]
# EDITOR: [OPERATOR, EXPORT]
# ADMIN: [MASTER]
# admin_permissions: [CREATE, LIST, DELETE, UNDELETE, EXPORT, OPERATOR, MASTER]
# object_permissions: [VIEW, EDIT, DELETE, UNDELETE, OPERATOR, MASTER, OWNER]
dashboard:
blocks:
- { type: sonata.admin.block.admin_list, position: left }
groups:
content:
label: Contenu
icon: '<i class="fa fa-file-text-o"></i>'
items:
- app.admin.specialty
- app.admin.cities
- app.admin.colleges
- app.admin.building
sonata.admin.group.media:
label: Médiathèque
icon: '<i class="fa fa-camera-retro"></i>'
items:
- sonata.media.admin.media
settings:
label: Paramètres
icon: '<i class="fa fa-cog"></i>'
items:
- sonata.classification.admin.category
- sonata.classification.admin.context
- sonata.classification.admin.tag
- sonata.classification.admin.collection
- app.admin.icon
sonata.admin.group.administration:
label: Utilisateur et Groupes
label_catalogue: SonataAdminBundle
icon: '<i class="fa fa-users"></i>'
items:
- app.admin.user
- sonata.user.admin.group
sonata_block:
blocks:
sonata.admin.block.admin_list:
contexts: [admin]
sonata_user:
security_acl: true
manager_type: orm
class:
user: App\Application\Sonata\UserBundle\Entity\User
group: App\Application\Sonata\UserBundle\Entity\Group
fos_user.yaml file:
fos_user:
db_driver: orm # valid values are 'orm', 'mongodb' and 'couchdb'
user_class: App\Entity\User #App\Application\Sonata\UserBundle\Entity\User
firewall_name: main
registration:
form:
type: App\Application\Sonata\UserBundle\Form\RegistrationType
group:
group_class: App\Application\Sonata\UserBundle\Entity\Group
group_manager: sonata.user.orm.group_manager
service:
user_manager: sonata.user.orm.user_manager
mailer: fos_user.mailer.noop
from_email:
address: "%env(MAILER_SENDER_ADDRESS)%"
sender_name: "%env(MAILER_SENDER_NAME)%"
If you want to access to your method getGenderList() from configureFormFields() you can do that :
protected function configureFormFields(FormMapper $formMapper)
{
$now = new \DateTime();
$genderOptions = [
'choices' => $this->getSubject()->getGenderList(), // here
'required' => true,
'translation_domain' => $this->getTranslationDomain(),
];
// NEXT_MAJOR: Remove this when dropping support for SF 2.8
if (method_exists(FormTypeInterface::class, 'setDefaultOptions')) {
$genderOptions['choices_as_values'] = true;
}
}
See the Symfony docs about it.

Sonata User Advanced Configuration

I am trying to configure the advanced configuration of the SonataUser Bundle(https://sonata-project.org/bundles/user/3-x/doc/reference/advanced_configuration.html).
After following the documentation I get the following errors on installing assets:
[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "jms_serializer" (in /var/www/html/SonataProject/app/config/config.yml). Looked for
namespace "jms_serializer", found "framework", "security", "twig", "monolog", "swiftmailer", "doctrine", "sensio_framework_extra", "fos_use
r", "sonata_core", "sonata_block", "knp_menu", "sonata_doctrine_orm_admin", "sonata_admin", "sonata_easy_extends", "sonata_user", "debug", "
web_profiler", "sensio_distribution" in /var/www/html/SonataProject/app/config/config.yml (which is being imported from "/var/www/html/Sonat
aProject/app/config/config_dev.yml").
After I comment out jms_serializer configuration, I get another error:
[Symfony\Component\Config\Definition\Exception\InvalidTypeException]
Invalid type for path "sonata_user.profile.dashboard.blocks.position". Expected array, but got string
Here is my config.yml:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Doctrine Configuration
doctrine:
dbal:
types:
json: Sonata\Doctrine\Types\JsonType
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
# naming_strategy: doctrine.orm.naming_strategy.underscore
# auto_mapping: true
entity_managers:
default:
mappings:
ApplicationSonataUserBundle: ~
SonataUserBundle: ~
FOSUserBundle: ~
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
#FOSUser Configuration
fos_user:
db_driver: orm
firewall_name: main
user_class: Application\Sonata\UserBundle\Entity\User
group:
group_class: Application\Sonata\UserBundle\Entity\Group
group_manager: sonata.user.orm.group_manager
profile:
# Authentication Form
form:
type: fos_user_profile
handler: fos_user.profile.form.handler.default
name: fos_user_profile_form
validation_groups: [Authentication]
service:
user_manager: sonata.user.orm.user_manager
#SonataUser
sonata_user:
security_acl: false
manager_type: orm
table:
user_group: "my_custom_user_group_association_table_name"
impersonating:
route: page_slug
parameters: { path: / }
class:
user: Sonata\UserBundle\Entity\User
group: Sonata\UserBundle\Entity\Group
admin: # Admin Classes
user:
class: Sonata\UserBundle\Admin\Entity\UserAdmin
controller: SonataAdminBundle:CRUD
translation: SonataUserBundle
group:
class: Sonata\UserBundle\Admin\Entity\GroupAdmin
controller: SonataAdminBundle:CRUD
translation: SonataUserBundle
profile:
# As in SonataAdminBundle's dashboard
dashboard:
groups:
# Prototype
id:
label: ~
label_catalogue: ~
items: []
item_adds: []
roles: []
blocks:
type: ~
settings:
# Prototype
id: []
position: right
register:
# You may customize the registration forms over here
form:
type: sonata_user_registration
handler: sonata.user.registration.form.handler.default
name: sonata_user_registration_form
validation_groups:
# Defaults:
- Registration
- Default
# This allows you to specify where you want your user redirected once he activated his account
confirm:
redirect:
# Set it to false to disable redirection
route: 'sonata_user_profile_show'
route_parameters: ~
# Customize user portal menu by setting links
menu:
- { route: 'sonata_user_profile_edit', label: 'link_edit_profile', domain: 'SonataUserBundle'}
- { route: 'sonata_user_profile_edit_authentication', label: 'link_edit_authentication', domain: 'SonataUserBundle'}
# Profile Form (firstname, lastname, etc ...)
form:
type: sonata_user_profile
handler: sonata.user.profile.form.handler.default
name: sonata_user_profile_form
validation_groups: [Profile]
# override FOSUser default serialization
jms_serializer:
metadata:
directories:
- { path: %kernel.root_dir%/../vendor/sonata-project/user-bundle/Sonata/UserBundle/Resources/config/serializer/FOSUserBundle, namespace_prefix: 'FOS\UserBundle' }
#SonataAdmin
sonata_block:
default_contexts: [cms]
blocks:
# enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
sonata.user.block.menu: # used to display the menu in profile pages
sonata.user.block.account: # used to display menu option (login option)
sonata.block.service.text:
sonata.block.service.rss:
Any ideas??
EDIT:
For the second error I tried editing the sonata_user.profile.dashboard.blocks as follows:
blocks:
type: ~
settings:
position: right
# Prototype
id: []
I am not sure if this is correct configuration but the error goes away.
Awaiting response!
Look at the doc the position is indented differently :
blocks:
type: ~
settings:
# Prototype
id: []
position: right
In yours, position is just under the id... Might be that

cannot load Twig template (Symfony 2.8.8)

So I have the folder structure below:
According to the Symfony manual (click here), I should be able to load my Twig template but Symfony cannot find it.
Any ideas what I could be doing wrong? Thank you.
codes I've tried
return $this->render(
"AppBundle:DisplayForm.html.twig"
, array(
"form" => $form->createView()
);
return $this->render(
"DisplayForm.html.twig"
, array(
"form" => $form->createView()
)
);
Contents of my app/AppKernel.php
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
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 Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
}
contents of app/config/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
assetic:
debug: '%kernel.debug%'
use_controller: '%kernel.debug%'
filters:
cssrewrite: ~
Because you are storing the template inside the bundle, you also have to reference the directory within the views - in this case, a 'null' directory.
AppBundle::DisplayForm.html.twig
Note the :: for the directory.
If you moved the entire AppBundle/Resources/views/ directory to app/Resources/views/ then you would be able to reference them much as a plain subdirectory:
Referencing templates stored in:
app/Resources/views/ | AcmeDemoBundle/Resources/views
----------------------------- | -------------------------------
index.html.twig | AcmeDemoBundle::index.html.twig
Default/subdir/index.html.twig | AcmeDemoBundle:Default:subdir/index.html.twig
Default/subdir/index.html.twig | AcmeDemoBundle:Default/subdir:index.html.twig
Probably you have forgotten an # symbol:
return $this->render(
"#AppBundle/DisplayForm.html.twig", [
"form" => $form->createView(
]
);

Sonata Admin extending User admin class and not using generated bundle

I had some trouble extending user admin when i tried to use the classes in my bundle and not in the generated bundle (Application/Sonata/UserBundle).
I don't know if my solution is good but it works for me.
Here is the procedure:
First the config:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
framework:
#esi: ~
#translator: { fallback: "%locale%" }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
# added for sonata user
types:
json: Sonata\Doctrine\Types\JsonType
orm:
auto_generate_proxy_classes: "%kernel.debug%"
# added for sonata user
entity_managers:
default:
mappings:
SonataUserBundle: ~
FOSUserBundle: ~
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
# FosRest configuration
fos_rest:
view:
formats:
rss: false
xml: false
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
# SonataAdmin configuration
sonata_block:
default_contexts: [cms]
blocks:
# Enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
sonata.user.block.menu: # used to display the menu in profile pages
sonata.user.block.account: # used to display menu option (login option)
sonata_user:
security_acl: false
manager_type: orm # can be orm or mongodb
class:
user: Wf\Bundle\TestsBundle\Entity\User
group: Wf\Bundle\TestsBundle\Entity\Group
admin:
user:
class: Wf\Bundle\TestsBundle\Admin\UserAdmin
controller: SonataAdminBundle:CRUD
translation: SonataUserBundle
group:
class: Wf\Bundle\TestsBundle\Admin\GroupAdmin
controller: SonataAdminBundle:CRUD
translation: SonataUserBundle
fos_user:
db_driver: orm # can be orm or odm
firewall_name: main
user_class: Wf\Bundle\TestsBundle\Entity\User
# use this option when easy extending the bundle (app/console sonata:easy-extends:generate SonataUserBundle --dest=src)
#user_class: Sonata\UserBundle\Entity\BaseUser
group:
group_class: Wf\Bundle\TestsBundle\Entity\Group
#group_class: Sonata\UserBundle\Entity\BaseGroup
group_manager: sonata.user.orm.group_manager
service:
user_manager: sonata.user.orm.user_manager
UserAdmin class; same with GroupAdmin class
<?php
/**
* Created by razvan.
* Date: 1/16/15
* Time: 1:53 PM
*/
namespace Wf\Bundle\TestsBundle\Admin;
use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use FOS\UserBundle\Model\UserManagerInterface;
use Sonata\AdminBundle\Route\RouteCollection;
class UserAdmin extends BaseUserAdmin
{
/**
* {#inheritdoc}
*/
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->with('General')
->add('username')
->add('email')
->end()
// .. more fields
;
}
/**
* {#inheritdoc}
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('User data')
->add('username')
->add('email')
->add('plainPassword', 'text', array('required' => false))
->end()
// .. more fields
;
if (!$this->getSubject()->hasRole('ROLE_SUPER_ADMIN')) {
$formMapper
->with('Management')
->add('roles', 'sonata_security_roles', array(
'expanded' => true,
'multiple' => true,
'required' => false
))
->add('locked', null, array('required' => false))
->add('expired', null, array('required' => false))
->add('enabled', null, array('required' => false))
->add('credentialsExpired', null, array('required' => false))
->end()
;
}
}
/**
* {#inheritdoc}
*/
protected function configureDatagridFilters(DatagridMapper $filterMapper)
{
$filterMapper
->add('id')
->add('username')
->add('locked')
->add('email')
;
}
/**
* {#inheritdoc}
*/
protected function configureListFields(ListMapper $listMapper)
{
// overide defaults
}
}
User class; same wirh Group class
<?php
namespace Wf\Bundle\TestsBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* Class User
* #package Wf\Bundle\TestsBundle\Entity
*
* #ORM\Table(name="fos_user_user")
* #ORM\Entity()
*/
class User extends BaseUser
{
/**
* #var integer $id
*
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
protected $id;
/**
* Get id
*
* #return integer $id
*/
public function getId()
{
return $this->id;
}
}
Now in Resources\config create a folder named "doctrine" and then add the folowing files:
First file is named Group.orm.xml
Inser the following into the file
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Wf\Bundle\TestsBundle\Entity\Group" table="fos_user_group">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>
</doctrine-mapping>
Second file is User.orm.xml
Insert the following:
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Wf\Bundle\TestsBundle\Entity\User" table="fos_user_user">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>
</doctrine-mapping>
Be careful to change the path to your own and clear the cache.

Resources