This example describes the session of Symfony2 and the use of cookies. Share to everyone for your reference, specific as follows:
Session Action:
1. Set session:
Public Function testsetsession () {
$session = $this->getrequest ()->getsession ();
$session->set ($sessionName, $sessionValue);
2. Get session:
Public Function testgetsession () {
$session = $this->getrequest ()->getsession ();
$username = $session->get ($sessionName);
3. Clear session:
Public Function testclearsession () {
$session = $this->getrequest ()->getsession ();/clear Session
$ Session->clear ();
}
Cookie action:
1. Set Cookie
Use Symfony\component\httpfoundation\response;
Use Symfony\component\httpfoundation\cookie;
Public Function Testsetcookie ($name, $value, $expire =0) {
$response = new Response ();
$response->headers->setcookie (New Cookie ($name, $value, Time () + $expire));
$response->send (); Includes Sendheaders (), Sendcontent ()
}
2. Get Cookie:
Public Function Testgetcookie () {
$request = $this->getrequest ();
return $request->cookies->all ();
}
3. Clear Cookie:
Public Function Testclearcookie () {
$response = new Response ();
$response->headers->setcookie (New Cookie ($name, $value,-1));
$response->send ();
}
4. Twig Template Invocation Cookie:
{{App.request.cookies.get (' Cookie_name ')}}
I hope this article will help you with your PHP programming based on the Symfony framework.