I injected the services.yml like that
services:
memcache:
class: Memcache
calls:
- [ addServer, [ %session_memcached_host%, %session_memcached_port% ]]
session.handler.memcache:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
arguments: [#memcache]
and my config.yml
handler_id: session.handler.memcache
and my php.ini
extension="memcache.so"
session.save_handler= memcache
session.save_path= tcp://127.0.0.1:11211
and i installed memcached using this link
but the problem when i load fosbundle login page i get this error
Attempted to load class "Memcache" from the global namespace.
Did you forget a "use" statement?
An easy way to integrate symfony with memcached is to use an existing bundle. I use leaseweb/memcache-bundle and it's super simple - just follow the instructions here: https://github.com/LeaseWeb/LswMemcacheBundle. And remember to start memcached first ;)
Yet to integrate this fully with FOSUserBundle, however it does support php sessions.
Related
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.
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"
I have a site built on Symfony framework and I had to add a legacy php script launched directly in the web directory.
I also have to share session's data between the Symfony framework controllers and the stand-alone php script.
Sessions on my server and the Symfony framework are configured to be in a memcached server (127.0.0.1 11211).
Here are my Symfony config files :
parameters.yml
session_memcached_host: 127.0.0.1
session_memcached_port: 11211
session_memcached_prefix: 'BPY_'
session_memcached_expire: 3600
config.yml
imports:
- { resource: session.yml }
...
session:
handler_id: session.handler.memcached
session.yml
services:
session.memcached:
class: Memcache
arguments:
persistent_id: %session_memcached_prefix%
calls:
- [ addServer, [ %session_memcached_host%, %session_memcached_port% ]]
session.handler.memcached:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
arguments: [#session.memcached, { prefix: %session_memcached_prefix%, expiretime: %session_memcached_expire% }]
Session in the Symfony controllers are set and read using :
$request->getSession->get('key');
$request->getSession->set('key', $value);
In the stand-alone old php script, I use the global session array to set and get data. And in theory, to allow Symfony and my script share data I set and get data in :
$_SESSION['_sf2_attributes']
So, finally with this configuration, when I set data in the symfony controllers I do not see it in the stand-alone php script and when I set data in the php script I do not see it in the Symfony controllers.
What is wrong in my configuration or my code?
You should check the Bridge a legacy Application with Symfony Sessions
but overall you can configure it in the following way:
framework:
session:
storage_id: session.storage.php_bridge
handler_id: ~
I finally solved the problem.
In the parameters.yml file I put :
session_memcached_prefix : ''
instead of :
session_memcached_prefix : 'BPY_'
and the two envirennements can share sessions data.
In the Symfony controllers I still use
$request->getSession->get('key');
$request->getSession->set('key', $value);
And in the stand-alone php script I use set and get data in this array :
$_SESSION['_sf2_attributes']
So
$_SESSION['_sf2_attributes']['key'] = $value
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.
I'm facing an issue while trying to use Curl in Buzz for Symfony2 (I've finally managed to install it, see this post).
I use it it one on my bundles, and I've updated services.yml, adding these :
# cURL client for Buzz
buzz.client.curl:
class: Buzz\Client\Curl
public: false
calls:
- [setVerifyPeer, [false]]
# Buzz browser
buzz.browser:
class: Buzz\Browser
arguments: ['#buzz.client.curl']
And when I go and check my project's page, here's the error I get :
InvalidArgumentException: There is no extension able to load the configuration for "buzz.client.curl" (in myBundle) Looked for namespace "buzz.client.curl", found none
So from what I understand, I have to change one of Buzz's namespace declarations somewhere.
But does anybody know what, and where?
Well, my mistake, I simply didn't use the right format for my yml file.
Here is the code of the services, in case anyone has the issue. Think of the 4 spaces, that saves a lot of time...
services:
cURL client for Buzz
buzz.client.curl:
class: Buzz\Client\Curl
public: false
calls:
- [setVerifyPeer, [false]]
Buzz browser
buzz.browser:
class: Buzz\Browser
arguments: ['#buzz.client.curl']