Symfony2 controller, symfony2

Source: Internet
Author: User

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 %}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.