doulin6448 2017-09-28 23:30
浏览 85

Symfony:请求$ request对象在特定函数中不起作用

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条回答 默认 最新

  • duanfen9983 2017-09-29 07:25
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100