Symfony 3.1 PSR-6 Caching Settings - php

Config.yml:
cache:
app: cache.adapter.doctrine
system: cache.adapter.doctrine
default_doctrine_provider: ~
default_psr6_provider: ~
default_redis_provider: "redis://localhost:6379"
Symfony 3.1 support doctrine cache, but you do not have enough documentation.
Cache Component: http://symfony.com/doc/current/components/cache.html
Supported drives: http://symfony.com/doc/current/components/cache/cache_pools.html
Symfony Integration: http://symfony.com/blog/new-in-symfony-3-1-cache-component
default_doctrine_provider: ? What do I enter as Provider

You can pass to default_doctrine_provider either a Redis connection DSN (for example "redis://127.0.0.1:6379") or ID of a service which implements Symfony\Component\Cache\Adapter\AdapterInterface
You can have a look at already implemented adapters here

The provider basically is the original doctrine_cache provider you configured. Let's say you use the DoctrineCacheBundle and your provider name is my_apc_cache that means the container has the following service:
$myCache = $this->container->get('doctrine_cache.providers.my_apc_cache');
You could also define an alias, then it is even easier.
Take a look at the example at: https://symfony.com/doc/current/bundles/DoctrineCacheBundle/usage.html#service-aliases

Related

Configuring which Redis adapter to use in Symfony Cache

I want to make use of Predis\Client instead of \Redis for all the Redis connections.
The Symfony docs on cache adapters describe that you can give additional options to the createConnection method.
However, this is all autowired in the service container. The only thing I'm declaring is that I want to use Redis for caching:
framework:
cache:
app: cache.adapter.redis
default_redis_provider: '%redis_dsn%'
Is there any way I can configure the default options for the RedisAdapter? Or is there another way that I can set Symfony always to use Predis\Client for Redis?
Configuring the DSN with ?class=\Predis\Client works, is this the optimal solution?
There's nothing wrong with adding additional options to the DSN. Being able to configure your provider with just a string is why it exists. However you can define a custom provider service and use whatever configuration you'd like.
From https://symfony.com/doc/current/cache.html#custom-provider-options:
# config/packages/cache.yaml
framework:
cache:
pools:
cache.my_redis:
adapter: cache.adapter.redis
provider: app.my_custom_redis_provider
services:
app.my_custom_redis_provider:
class: \Redis
factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
arguments:
- 'redis://localhost'
- { retry_interval: 2, timeout: 10 }
In your case you'd change the class to Client\Predis and change the applicable settings.

Symfony 3.4 controller registered as service throws deprecation warning

I have a controller, let's say Acme\ShopBundle\Controller\ProductListController
And its definition in services.yml is as follows:
services:
Acme\ShopBundle\Controller\ProductListController:
class: Acme\ShopBundle\Controller\ProductListController
arguments: ['#product_service']
Which throws this in my log file:
User Deprecated: The "Acme\ShopBundle\Controller\ProductListController" service is private, checking for its existence is deprecated since Symfony 3.2 and will fail in 4.0.
Followed by
User Deprecated: The "Acme\ShopBundle\Controller\ProductListController" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.
The stack trace list of files is completely inside vendor/symfony so I'm assuming something is misconfigured, but stumped as to what. Any help appreciated.
Controller service must be public:
services:
Acme\ShopBundle\Controller\ProductListController:
public: true
arguments: ['#product_service']
Why aren't you using autowiring anyway? You could register all of your controllers then:
Acme\ShopBundle\Controller\:
resource: '../src/Acme/ShopBundle/Controller' # mutatis mutandis
tags: ['controller.service_arguments']
Kindly read about new features regarding dependency management in Symfony 3.

how to inject "memcache" to service.yml?

I installed memcache lib and added it to
framework:
session:
handler_id: session.handler.memcache
but when I trying to use it I get this error
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "session.handler.memcache".
You want to use memcache or memcached?
These are two different extensions, so be aware of that.
And I suggest to use memcached, memcache is dead.
Serivce session.handler.memcache is not defined, so you have to define one implementing SessionHandlerInterface, in your case MemcacheSessionHandler.
First, we need to define memcache instance as a service, so we can pass it to MemcacheSessionHandler constructor:
memcache:
class: \Memcache
calls:
- [ addServer, [ %host_parameter%, %port_parameter% ]]
Then, your session handler:
session.handler.memcache:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
arguments: [#memcache]
You can also use a bundle like cache/adapter-bundle to register a PSR-6 compatible service (or even a symfony cache component, introduced in 3.1) and use Psr6SessionHandler.
If you want to use memcached, it's almost the same configuration-wise.
Symfony has it's own component: https://symfony.com/doc/current/components/cache.html
You have to configure it first in your /config/packages/framework.yaml:
framework:
cache:
pools:
memcached_service:
adapter: cache.adapter.memcached
public: true
provider: 'memcached://memcached:11211'
Now you can inject your Memcached service wherever you want (services.yaml):
App\Service\SomeService:
arguments:
- "#memcached_service"

Symfony CMF: Unrecognized option "use_sonata_admin" under "cmf_routing.dynamic.persistence.phpcr"

I try to install Symfony CMF with SonataDoctrinePHPCRAdminBundle() and CmfTreeBrowserBundle().
According to docs there's an option in app/config.yml to prevent administrative routes be view-able in admin interface:
# app/config/config.yml
cmf_routing:
# ...
dynamic:
# ...
persistence:
phpcr:
# ...
use_sonata_admin: false
But I receive an error both in the console and on the site:
"Unrecognized option "use_sonata_admin" under
"cmf_routing.dynamic.persistence.phpcr"
What could it be?
we are in the process of moving to cmf version 2. the tutorial is currently outdated. sonata admin is now all configured at the new admin bundle, not on cmf_routing anymore. if you want something that works as documented, please use the 1.3 release and the 1.3 documentation. if you are trying out new things, see github.com/symfony-cmf/symfony-cmf-docs/milestone/4 for what documentation can be expect to work or not yet. any help in updating the doc is appreciated of course ;-)

logging in symfony 2.3

I am trying to write my own messages to the log in Symfony 2.3, from anywhere, and not just the Controller (which I realize you can just do a "$this->get('logger')".
I've seen that in Symfony 1 you can use sfContext, but that class no longer seems to be a viable choice in 2.3.
Any help is appreciated.
Symfony2 has Service-oriented architecture (http://en.wikipedia.org/wiki/Service-oriented_architecture) and logger is one of service (by default Monolog). In controller you have access to service via $this->get('service_name'). Here is more info about service container: http://symfony.com/doc/current/book/service_container.html#what-is-a-service-container. If you wanna use logger in another service you have to define service and inject logger service. Example:
# section with defined service in your config.yml file (by default in config.yml)
services:
# your service name
my_service:
# your class name
class: Fully\Qualified\Loader\Class\Name
# arguments passed to service constructor. In this case #logger
arguments: ["#logger"]
# tags, info: http://symfony.com/doc/current/components/dependency_injection/tags.html
tags:
- { name: monolog.logger, channel: acme }
Additionally you should familiarize with dependency injection docs: http://symfony.com/doc/current/components/dependency_injection/index.html
I hope that helped. If not, please let me know where exactly you want to use logger.

Categories