The documentation is broken and leads to a 404 page. I'm not too good at reverse engineering classes like this, any tips on how to setup services.yml to use it?
Doc page
https://symfony.com/doc/current/components/http_foundation/session_configuration.html
Session Handler 404
https://api.symfony.com/4.1/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.html
Git Page for Session Handler
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php
You need to define 2 services:
One for the Redis connection.
One for RedisSessionHandler that will make use of that connection.
Edit the services file:
# config/services.yaml
services:
Redis:
class: Redis
calls:
- method: connect
arguments:
- '%env(REDIS_HOST)%'
- '%env(int:REDIS_PORT)%'
# If you need key prefix
# - method: setOption
# arguments:
# - !php/const Redis::OPT_PREFIX
# - 'my_prefix'
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments:
- '#Redis'
(note that I stored here the Redis host & port as environment variables, but you can define them elsewhere if needed).
You can now make use of the service as your session handler:
# config/packages/framework.yaml
framework:
session:
handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
I struggled a bit too, and wrote a more detailed articled: http://blog.michaelperrin.fr/2018/08/14/redis-session-handling-in-symfony/
For adding custom prefix, maybe the following is better:
Redis:
class: Redis
calls:
- method: connect
arguments:
- '%env(REDIS_HOST)%'
- '%env(int:REDIS_PORT)%'
# - method: setOption
# arguments:
# # #see https://symfony.com/blog/new-in-symfony-3-2-php-constants-in-yaml-files
# - !php/const Redis::OPT_PREFIX
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments:
- '#Redis'
# #see https://symfony.com/doc/current/components/yaml/yaml_format.html
-
prefix: ivannotes_
Related
We have a new Symfony setup with Redis as cache mechanism. We want to connect to a specific host, not the default localhost. On production, the ./bin/console debug:dotenv gives the correct REDIS_HOST. This is configured in our .env and .env.local.php.
The error we get is:
Connection refused: tcp:127.0.0.1/6379
This is our config:
services.yml
services:
Redis:
# you can also use \RedisArray, \RedisCluster or \Predis\Client classes
class: \Predis\Client
calls:
- connect:
- '%env(REDIS_HOST)%'
- '%env(int:REDIS_PORT)%'
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments:
- '#Redis'
- prefix: sp_ss_
- ttl: 1800
cache.yml
framework:
cache:
app: cache.adapter.redis
default_redis_provider: 'Redis'
pools:
site.cache:
adapter: cache.app
And our .env file:
APP_ENV=prod
APP_SECRET=****
MESSENGER_TRANSPORT_DSN=redis://redis.local:6379/messages
REDIS_HOST=redis.local
REDIS_PORT=6379
REDIS_URL=redis://redis.local:6379
The Symfony's documentation suggest to use "calls -> connect", but it's used only when you was defined the class as 'Redis'. When you use '\Predis\Client', you need to use the settings bellow:
"config/services.yaml"
Redis:
# you can also use \RedisArray, \RedisCluster or \Predis\Client classes
class: \Predis\Client
# See all parameters here: https://github.com/predis/predis/wiki/Connection-Parameters#list-of-connection-parameters
arguments:
- host: '%env(REDIS_HOST)%'
- port: '%env(int:REDIS_PORT)%'
# uncomment the following if your Redis server requires a password
# - password: '%env(REDIS_PASSWORD)%'
I'm also using the '\Predis\Client' and after to change to 'arguments' the connection was worked here.
For more parameters references, please check this link (List of connection parameters).
There might be hidden other .env files. May you check out the files list with the following code at the root of your projects? Are there other env files such as .env.prod, .env.local?
ls -lah
When I import services in the main configuration:
imports:
- { resource: services/attribute_loaders.yaml }
The services in the included file will be replaced with an autoconfigured version, so I've missed all my configured tags:
services:
App\Infrastructure\Bridge\Doctrine\EventListener\AttributeLoader\OrderAttributeLoader:
autowire: true
tags:
- name: 'doctrine_mongodb.odm.event_listener'
entity: 'App\Infrastructure\Bridge\Doctrine\EventListener\AttributeLoader\OrderAttributeLoader'
event: 'postLoad'
As a workaround, I've excluded my service from autoconfigure.
Is there any way to decompose services into several files?
That's exactly how you do it.
If you are going to use multiple files to configure your services, you simply can't define the same service twice.
Your definitions need to be specific enough so they do not overlap.
If you are going to use broad resource settings, you will need to add all the corresponding excludes so services defined in different files are not defined before it's time:
services:
App\:
resource: '../src/*'
exclude:
- '../src/Infrastructure/Symfony/DependencyInjection'
- '../src/Infrastructure/Symfony/Kernel.php'
- '../src/Tests'
- '../src/Messenger'
Services on src/Messenger, that belong to the App\Messenger namespace, can be defined independently on a different file:
E.g. something like this:
# messenger_services.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false
App\Messenger\CommandHandler\:
resource: '../../src/Messenger/CommandHandler/*'
tags:
- {name: 'messenger.message_handler', bus: command.bus}
App\Messenger\EventHandler\:
resource: '../../src/Application/EventHandler/*'
tags:
- {name: 'messenger.message_handler', bus: event.bus}
I learning Symfony3 on https://knpuniversity.com. Now on create their own filters in Twig (https://knpuniversity.com/screencast/symfony-services/create-twig-extension). In this video the author creates a class filter, but when using this filter in the Twig template:
<dd>{{ genus.funFact|markdownify }}</dd>
Symfony crashes with the error, and the author of the video says that the filter has used need to say Symfony about new filter Twig'. This is done using the register Twig extension as service in the services.yml. But, I have it all working, without errors, without any registrations in service.yml.
Do steel need register Twig Extensions in the services.yml?
Or is now not required and determines its own Twig extension?
I have a Symfony 3.3.6 (in video used 3.0.0).
Here listing my service.yml:
parameters:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
AppBundle\Service\MarkdownTransformer:
arguments:
- '#markdown.parser'
- '#doctrine_cache.providers.my_markdown_cache'
public: true
# AppBundle\Twig\MarkdownExtension:
# arguments: ['#AppBundle\Service\MarkdownTransformer']
# tags:
# - { name: twig.extension }
# public: true
In the bottom I'm added the settings block, which registers a new TWIG extension. Without this block, Symfony throws an exception (in the author of the lesson). But I have everything working without this block. So for example, I have it commented out.
I am new to Symfony.I am trying to learn File upload in Symfony.I am getting below error:
(3/3) FileLoaderLoadException
The file "D:\wamp\www\symfony_project\app/config\services.yml" does not contain valid YAML in D:\wamp\www\symfony_project\app/config\services.yml (which is being imported from "D:\wamp\www\symfony_project\app/config\config.yml").
config.yml
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
services.yml
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
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
# 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'
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
public: true
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
FileUploader.php
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->getTargetDir(), $fileName);
return $fileName;
}
public function getTargetDir()
{
return $this->targetDir;
}
}
As the error says, services.yml is not valid YAML. Remove $ sign before targetDir in line:
$targetDir: '%brochures_directory%'
So it will be:
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
I guess that this is your issue, but I cannot be sure as I don't know how looks the whole content of this file.
your services.yml does not contain valid YAML (Error message can't be more clear ;) )
Try this kind of code :
/***/
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
admin.file_uploader:
class: AppBundle\Service\FileUploader
arguments: [~, '%brochures_directory%', ~]
Change
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
To
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
Should work now:
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
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
# 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:
- { name: "controller.service_arguments" }
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
public: true
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
You have space in front of AppBundle\Service\FileUploader:
→ ﹍AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
Hint: In future use Yaml linter:
bin/console lint:yaml app/config/services.yml
I am trying to register a custom Gateway Factory with the PayumBundle. If I register my Gateway Factory directly with the PayumBuilder it works fine. But if i add it via the PayumBundle all the extensions are missing.
# app/config/config.yml
payum:
gateways_v2:
superpay:
factory: superpay
service: ~
And my service configuration looks like this:
# app/config/services.yml
services:
app.superpay.factory:
class: App\Payum\Superpay\SuperpayGatewayFactory
arguments:
- { url: http://www.example.com } # change this
tags:
- { name: payum.gateway_factory, factory_name: superpay, human_name: Superpay }
Maybe it's related to https://github.com/Payum/Payum/issues/452
I use symfony 2.8, payum-core 1.2.2 and payum-bundle 1.2.3
Update: With payum-bundle 2.0 it works with this configuration
# app/config/config.yml
payum:
gateways:
superpay:
factory: superpay
url: http://www.example.com
and service configuration like
# app/config/services.yml
services:
app.superpay.factory
class: Payum\Core\Bridge\Symfony\Builder\GatewayFactoryBuilder
arguments: [App\Payum\Superpay\SuperpayGatewayFactory]
tags:
- { name: payum.gateway_factory_builder, factory: girosim }
I'd suggest to jump to payum bundle 2.x, it should not be hard and allows to solve your problem easier than you can do it in 1.x. In 2.x you have to register a gateway factory builder service with a tag, like this: https://github.com/makasim/PayumBundleSandbox/blob/master/app/config/payum.yml#L194
If you still want to stick to payum bundle 1.x you have to implement the factory from the bundle, like this one https://github.com/Payum/PayumBundle/blob/1.x/DependencyInjection/Factory/Gateway/PaypalExpressCheckoutNvpGatewayFactory.php
and register it in the bundle's extension like this https://github.com/Payum/PayumBundle/blob/1.x/PayumBundle.php#L39