Rumah php教程 PHP开发 Symfony2之session与cookie用法小结

Symfony2之session与cookie用法小结

Dec 26, 2016 am 11:35 AM

本文实例讲述了Symfony2之session与cookie用法。分享给大家供大家参考,具体如下:

session操作:

1. Set Session:

public function testSetSession() {
  $session = $this->getRequest()->getSession();
  $session->set($sessionName, $sessionValue );
}
Salin selepas log masuk

2. Get Session:

public function testGetSession() {
 $session = $this->getRequest()->getSession();
 $username = $session->get($sessionName);
}
Salin selepas log masuk

3. Clear Session:

public function testClearSession() {
  $session = $this->getRequest()->getSession();//清除session
  $session->clear();
}
Salin selepas log masuk

cookie操作:

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(); // 包括 sendHeaders()、sendContent()
}
Salin selepas log masuk

2. Get Cookie:

public function testGetCookie() {
 $request = $this->getRequest();
 return $request->cookies->all();
}
Salin selepas log masuk

3. Clear Cookie:

public function testClearCookie() {
 $response = new Response();
 $response->headers->setCookie(new Cookie($name, $value, -1));
 $response->send();
}
Salin selepas log masuk

4. twig模板调用cookie:

{{ app.request.cookies.get('cookie_name') }}
Salin selepas log masuk

   

希望本文所述对大家基于Symfony框架的PHP程序设计有所帮助。

更多Symfony2之session与cookie用法小结相关文章请关注PHP中文网!


Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)