Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将多个值作为数组添加到Symfony会话?_Php_Arrays_Symfony - Fatal编程技术网

Php 如何将多个值作为数组添加到Symfony会话?

Php 如何将多个值作为数组添加到Symfony会话?,php,arrays,symfony,Php,Arrays,Symfony,我正在尝试使用Ajax在Syfmony中创建一个基本的“添加到篮子”功能。到目前为止,我有: /** * @Route("/basket/add") */ public function addAction(Request $request) { $item = [ 'id' => $request->get('id'), 'artist' => $request->get('artist'), 'tit

我正在尝试使用Ajax在Syfmony中创建一个基本的“添加到篮子”功能。到目前为止,我有:

/**
 * @Route("/basket/add")
 */
public function addAction(Request $request)
{
    $item = [
        'id'     => $request->get('id'),
        'artist' => $request->get('artist'),
        'title'  => $request->get('title'),
        'type'   => $request->get('type')
    ];

    $this->session->set('basket-'.$item['id'], $item);

    return new JsonResponse($this->session->all());
}
但理想情况下,我会为篮筐安排一次训练。我之前尝试过使用
array\u push
一个接一个地追加值,但没有走多远

有什么建议吗


干杯

我认为use应该首先调用session类,首先初始化session,然后尝试使用将值作为数组添加到session中。 试试下面的代码,希望这对你有用

试试这个:

public function addSesAction(Request $request)
{
 $session = new session();
 $item = [
   'id'     => $request->get('id'),
    'artist' => $request->get('artist'),
    'title'  => $request->get('title'),
    'type'   => $request->get('type')
 ];

 $session->set('basket-'.$item['id'], $item);

 return new JsonResponse($session->all());
}

你试过这样的吗

public function addSesAction(Request $request)
{
    $basket = $request->getSession()->get('basket', []);
    array_push($basket, [
        'id'     => $request->get('id'),
        'artist' => $request->get('artist'),
        'title'  => $request->get('title'),
        'type'   => $request->get('type')
    ]);
    $request->getSession()->set('basket', $basket);

    return new JsonResponse($basket);
}

什么是
$this->session
?它不应该是
$request->getSession()
?而且
$request->get()
也不是推荐的方式,它可能会很慢。这正是我想要的。非常感谢你!