01/2015 / Milan "perun" Herda / @moriquend / hrad.perunhq.org
{
"require": {
"symfony/http-foundation": "~2.6.3"
}
}
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
$id = array_key_exists($_GET['id']) ? $_GET['id'] : 0;
$uri = $_SERVER['REQUEST_URI'];
$hostname = $_SERVER['HTTP_HOST'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$id = $request->query('id', 0);
$uri = $request->getPathInfo();
$hostname = $request->getHost();
$agent = $request->headers->get('User-Agent')
$_GET | $request->query |
$_POST | $request->request |
$_COOKIE | $request->cookies |
$_FILES | $request->files |
$_SERVER | $request->server |
http hlavičky | $request->headers |
iné | $request->attributes |
Každá z vlastností query, request, cookies, files, server, headers a attributes je public premenná typu ParameterBag
public function get($key, $default = null, $deep = false)
$request->getSession()
Volanie vráti inštanciu SessionInterface
Už nemusíme vymýšľať, ako otestovať kontroler, ktorý pracuje s $_GET, $_POST. Vytvoríme si Request a pošleme ho kontroleru.
$request = Request::create(
'/hello-world',
'GET',
[
'name' => 'Fabien'
]
);
header('HTTP/1.0 404 Not Found');
echo 'oops';
use Symfony\Component\HttpFoundation\Response;
$response = new Response(
'oops',
Response::HTTP_NOT_FOUND,
[
'content-type' => 'text/html',
]
);
$response->send();
header('HTTP/1.0 404 Not Found');
echo 'oops';
use Symfony\Component\HttpFoundation\Response;
$response = new Response(
'oops',
Response::HTTP_NOT_FOUND,
['content-type' => 'text/html']
);
//...
$response->setContent('tak sme predsa našli')
$response->setStatusCode(Response::HTTP_OK);
$response->headers->set('Content-Type', 'text/plain');
//...
$response->send();
use Symfony\Component\HttpFoundation\Cookie;
$response->headers->setCookie(new Cookie('foo', 'bar'));
$response->setCache([
'etag' => 'abcdef',
'last_modified' => new \DateTime(),
'max_age' => 600,
's_maxage' => 600,
'private' => false,
'public' => true,
]);
Jednotlivé cachovacie hlavičky sa dajú nastaviť aj samostatne.
Metóda isNotModified skontroluje hlavičky a pokiaľ nedošlo k zmene dokumentu, tak nastaví status 304 a odstráni telo odpovede.
if ($response->isNotModified($request)) {
$response->send();
}
use Symfony\Component\HttpFoundation\RedirectResponse;
$response = new RedirectResponse('http://example.com/');
use Symfony\Component\HttpFoundation\JsonResponse;
$response = new JsonResponse();
$response->setData([
'data' => 123
]);
use Symfony\Component\HttpFoundation\BinaryFileResponse;
$file = 'path/to/file.pdf';
$response = new BinaryFileResponse($file);
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->start();
$session->set('username', 'Fero');
$session->get('username');
$session->getFlashBag()->add('notice', 'Vaša objednávka bola spracovaná');
foreach ($session->getFlashBag()->get('notice', []) as $msg) {
echo '<div class="flash-notice">' . $msg . '</div>';
}
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
// unit testy, ktoré nepotrebujú ukladať session
$session = new Session(new MockArraySessionStorage());
// funkcionálne testy, ktoré potrebujú perzistované session dáta
$session = new Session(new MockFileSessionStorage());
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
// legacy application configures session
ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();
// Get Symfony to interface with this existing session
$session = new Session(new PhpBridgeSessionStorage());
// symfony will now interface with the existing PHP session
$session->start();