1
votes

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.

1
$Authuser = $this->isLoggedIn($request); should do the trick though you might be better off using the Symfony security bundle. The reason controller actions work is that the framework spends extra effort analyzing actions and injecting dependencies like the $request object. Regular methods like the poorly named isLoggedIn are not processed. - Cerad
@Cerad Thanks! Yes it did the trick & works that way. But isn't there any proper solution for this if someone is not using Symfony security bundle? I'm a beginner, though I tried a lot but security bundle flew over my head, could not understand its usage properly, hence using traditional way. - Echoes
Your approach will work. The security bundle is complex though if you follow the example in the docs it's not too bad. Especially if you end up wanting to use roles and such. - Cerad

1 Answers

0
votes

You can't declare Request parameters for non action functions in controller. Try call isLoggedIn in an action with $request parameters without declare it in the definition of the function.

Hope it could help you.