Gets the Session.
public getSession ( ) : Symfony\Component\HttpFoundation\Session\SessionInterface | null | ||
return | Symfony\Component\HttpFoundation\Session\SessionInterface | null | The session |
use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $session = $request->getSession(); // Output the session ID echo $session->getId();
use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $session = $request->getSession(); // Store a value in the session $session->set('name', 'John'); // Retrieve the value from the session $name = $session->get('name'); // Output the value echo "Hello, {$name}!";In this example, we retrieve the session object using the getSession() method as before. Then, we store a value 'John' with key 'name' in the session using the set() method. Afterwards, we retrieve the value from the session using the get() method and output it using the echo statement. In conclusion, Request getSession() method is part of Symfony\Component\HttpFoundation package library and is used to retrieve the current session or manipulate session data.
public getSession ( ) : Symfony\Component\HttpFoundation\Session\SessionInterface | null | ||
return | Symfony\Component\HttpFoundation\Session\SessionInterface | null | The session |