How can I test if a session exists in symfony?
Without the framework we proceed like this:
<?php
if( isset($_SESSION['login'])) {
echo 'Bonjour '.$_SESSION['login'].' vous etes connecté.<br>';
}
?>
How to do so with the session service in the symfony controller with the Request object?
In Symfony, session service is an instance of Symfony\Component\HttpFoundation\Session\Session. That said, session does provide several method to either check or manipulate session.
In your particular case, you would do something like this:
$session = $request->getSession();
if ( $session->has('login')){
$message = 'Bonjour '.$session->get('login').' vous etes connecté.<br>';
}
But, noting the comment from @Gerry, his question is a very valid one.
Hope this helps...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With