I'm a beginner in Symfony & have wasted hours in this problem.
I am trying to call a function isLoggedIn
from another function of same class. In the isLoggedIn
function, I need to get/check a session variable and return the value of that variable. But the Request $request
object is somehow not accessible in the isLoggedIn
function, while the Request object works well in other functions of the same class.
My code:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use AppBundle\Entity\Users;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$Authuser = $this->isLoggedIn();
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
'Authuser' => $Authuser
]);
}
public function isLoggedIn(Request $request) ////GETTING ERROR IN THIS LINE////
{
$session = $request->getSession();
if($session->get('Authuser')!=null) $Authuser = $session->get('Authuser');
else $Authuser = null;
return $Authuser;
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction(Request $request)
{
$session = $request->getSession();
$session->remove('user_id');
return $this->redirectToRoute('homepage');
}
}
?>
Exact error that I'm getting:
Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Catchable Fatal Error: Argument 1 passed to AppBundle\Controller\DefaultController::isLoggedIn() must be an instance of Symfony\Component\HttpFoundation\Request, none given, called in /Applications/MAMP/htdocs/srfood/src/AppBundle/Controller/DefaultController.php on line 21 and defined"
I have searched a lot but getting no solutions, moreover I need to know what according to Symfony is different in isLoggedIn
function from the other functions of same class, because I tried adding a route to the function, adding Action suffix etc etc so that it behaves like rest of the functions, but I just can't understand how this function is different for symfony?! Thanks!
PS- I am using Symfony 3.3.