Symfony2 controller, symfony2
1. Basic Concepts
One http Request (Request): header information, get information, post data, etc.
Response: The information returned by symfony after processing, including the page, json string, and URL jump.
2. Request
$ This-> getRequest ()
Httpie Tool
HTTPie (read aych-tee-pie) is an HTTP command line client. The goal is to make the interaction between CLI and web services as user-friendly as possible.
Install reference http://blog.csdn.net/pzw_0612/article/details/46521965
Http://www.cnblogs.com/huangjacky/archive/2012/03/28/2421866.html
Simulate form submission (post) with httpie)
>http -f post http://localhost:8000/app_dev.php/page/test name=lily
3. Response
<? Phpnamespace Scource \ WebBundle \ Controller; use Symfony \ Bundle \ FrameworkBundle \ Controller; use Sensio \ Bundle \ FrameworkExtraBundle \ Configuration \ Route;Use Symfony \ Component \ HttpFoundation \ Response; // do not reference the errorUse Symfony \ Component \ HttpFoundation \ JsonResponse; use Symfony \ Component \ HttpFoundation \ RedirectResponse; class DefaultController extends Controller {/*** @ Route ("/page/test1 ") */public function test1Action (){
// If no template is used, directly output the content or jump to // return new RedirectResponse ('HTTP: // www.cnblogs.com/tianxintian22 /'); redirect // return new JsonResponse (array ('A' => 'abcdef '); return json string return new Response ('20140901 ');}}
4. session
$this->getRequest()->getSession()->set('b', 'ni hao!');$this->getRequest()->getSession()->get('b');
If the session value cannot be correctly obtained, it may be that the session permission in the app/cache/dev directory is incorrect.
// Call flashmessage, which can only be displayed once and is discarded. It is generally used in form user information prompts. // php
$ This-> getRequest ()-> getSession ()-> getFlashBag ()-> add ('notic', 'you have something wrong ');
// Twig
{% For flashMessage in app. session. flashbag. get ('notice ') %}
<Div class = "flash-notice">
{FlashMessage }}
</Div>
{% Endfor %}