Я пытаюсь сохранить куки, но они не работают. В чем проблема?
Класс обслуживания:
<?php
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
class MyUser
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack->getCurrentRequest();
}
// Adds a location to the user's wishlist
public function addToWishlist($locationId)
{
$cookies = $this->requestStack->cookies;
error_log(print_r($cookies, true));
$wishlist = $cookies->get('myWishlist', array());
if (!in_array($locationId, $wishlist))
{
$wishlist[] = $locationId;
}
$wishlistCookie = new Cookie('myWishlist', $wishlist, strtotime("+1 year"));
$response = new Response();
$response->headers->setCookie($wishlistCookie);
}
/**
* Returns an array of the user's wishlist IDs
*
* @return array
*/
public function getWishlistIDs()
{
$cookies = $this->requestStack->cookies;
return $cookies->get('myWishlist', array());
}
/**
* Returns a count of the number of items in the wishlist
*
* @return int
*/
public function getWishlistCount()
{
return count($this->getWishlistIDs());
}
/**
* Checks if a location is in this user's wishlist.
*
* @param $id A location ID
* @return boolean
*/
public function locationInWishlist($id)
{
$cookies = $this->requestStack->cookies;
$wishlist = $cookies->get('myWishlist', array());
return (in_array($id, $wishlist));
}
/**
* Removes a location from the user's wishlist,
* by location ID
*
* @param $id
* @return void
*/
public function removeFromWishlistById($id)
{
$cookies = $this->requestStack->cookies;
$id = abs(intval($id));
$wishlist = $cookies->get('myWishlist', array());
if (in_array($id, $wishlist))
{
$vals = array_flip($wishlist);
unset($vals[$id]);
$wishlist = array_flip($vals);
}
$wishlistCookie = new Cookie('myWishlist', $wishlist, strtotime("+1 year"));
$response = new Response();
$response->headers->setCookie($wishlistCookie); }
/**
* Removes all venues from the user's wishlist
*
* @return void
*/
public function removeAllFromWishlist()
{
$wishlistCookie = new Cookie('myWishlist', array(), strtotime("+1 year"));
$response = new Response();
$response->headers->setCookie($wishlistCookie);
}
}
Класс контроллера:
<?php
namespace AppBundle\Controller\Frontend;
use AppBundle\Entity\LocationStat;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Enquiry;
use AppBundle\Entity\EventType;
use AppBundle\Entity\PcawCodes;
use AppBundle\Entity\Setting;
use AppBundle\Entity\Thumbnail;
use AppBundle\Entity\WishlistCount;
use AppBundle\Form\Frontend\EnquiryForm;
use AppBundle\Service\MyUser;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Location;
use AppBundle\Entity\Wishlist;
use AppBundle\Service\UniqueValues;
use AppBundle\Form\Frontend\WishlistSaveEmailForm;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class WishlistsController extends Controller
{
/**
* @Route("wishlists/addVenueAjax/id/{id}", name="add_venue_ajax")
*/
public function addVenueAjaxAction(Request $request)
{
$doctrine = $this->getDoctrine();
if (!$request->isXmlHttpRequest())
return $this->redirectToRoute('homepage');
$id = $request->get("id", false);
$id = abs(intval($id));
if (!$id)
return new JsonResponse(array("error" => true, "render" => ""));
// Look up location. We can only add it if it's published etc.
$loc = $doctrine->getRepository(Location::class)->find($id);
if (!$loc || !$loc->getPublishedStatus())
return new JsonResponse(array("error" => true, "render" => ""));
// Add location to wishlist
$user = $this->get(MyUser::class);
$user->addToWishlist($id);
return new JsonResponse(array("error" => false, "newCount" => $user->getWishlistCount(), 'render' => $this->renderView('frontend/wishlists/partials/_itemwishlistaction.html.twig', array('locationid' => $loc->getId(),'user' => $user))));
}
}
Но я не вижу файлов cookie в разделе ответов профилировщика: