bharatesh
bharatesh

Reputation: 1022

Regarding Symfony2 Session

In symfony2 every user created controller extends Controller Class as shown below,

class MyController extends Controller {

thus functions related to session handling are available with $this object, But controllers in Vendor and Core don't extend Controller class thus don't provide access to session related functions. So is there any way to use these functions without extending Controller class.

Presently I am using $_SESSION[], for setting and getting session variables.

Is there any way other than above.

Upvotes: 0

Views: 97

Answers (1)

JamesHalsall
JamesHalsall

Reputation: 13475

Symfony2 provides a service for sessions, this is what you're trying to retrieve. All services in symfony2 are retrieved using the service container, which is what you're referring to with

$this->get('session');

To properly make use of the service container in your own controllers you can either...

  1. Configure your controllers as services (see: here)
  2. Extend the base Controller class provided by the Symfony2 stack (making the get() method available to your child Controller)

The first option is the correct way to go, you have full control over what services are then injected into your respective controllers (see service container documentation)

Upvotes: 2

Related Questions