1

Fairly new to PHP and Symfony: How do I gain access various session variable from within an include file that is included in AppKernel.php? I'm using Symfony 2.8. I've successfully tested the following, but it's not officially approved. I'd prefer to use Symfony objects to access the session.

session_start();
if (isset($session['userid'])) {
    $userid = $_SESSION['_sf2_attributes']['userid'];
}

I tried getting the session from the Symfony Request object using the following, but it looks like "Request::createFromGlobals" is creating another Request object vs. creating a Request object from an existing Request object.

use Symfony\Component\HttpFoundation\Request;

require_once '/var/www/html/vendor/autoload.php';

...

$request = Request::createFromGlobals();
$session = $request->get('session');

The $session variable returns null.

7
  • Could you please tell mewhich documentation involves such notions? I am trying to understand how, from inside AppKernel.php, such things can happen (Of course I tried first to help but found this a bit confusing) Commented Jul 19, 2016 at 21:07
  • 1
    hmmm... not sure that documentation exists. Technically, I've just added an include line to AppKernel.php, in the form of include foo.php'; So the heavy lifting is happening in foo.php. Commented Jul 19, 2016 at 21:09
  • If you're referring to the first option not being approved, there's this. symfony.com/doc/current/components/http_foundation/… "Symfony sessions are designed to replace several native PHP functions. Applications should avoid using session_start(), session_regenerate_id(), session_id(), session_name(), and session_destroy() and instead use the APIs in the following section." Commented Jul 19, 2016 at 21:12
  • I agree with you, it is just that I am used to dealing with Request and Session objects inside bundles/controllers, not inside appKernel.php (which I thought the role is to register the bundles or generally redefine Kernel methods ) Commented Jul 19, 2016 at 21:15
  • about your second comment, yes Symfony brings a lot of benefits regarding sessions than raw PHP Commented Jul 19, 2016 at 21:16

1 Answer 1

1

You can gain access to session variables in event listeners. Here is an example of a listener for the kernel.request event:

services:
    app.security:
        class: AppBundle\EventListener\Security
        arguments: [@session]
        tags:
        - { name: kernel.event_listener, event: kernel.request, method: checkAccess }

And part of the AppBundle\EventListener\Security class:

namespace AppBundle\EventListener;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class Security
{
    /**
     * @var \Symfony\Component\HttpFoundation\Session\Session
     */
    protected $session;

    /**
     * The constructor.
     *
     * @param Session         $session
     */
    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    /**
     * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
     */
    public function checkAccess(GetResponseEvent $event)
    {
        // Do what you like...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure if injecting the session will work as desired here since the listener will be constructed very early in the request processing. $event->getRequest()->getSession() might be a better approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.