search
Homephp教程php手册symfony2 controller,symfony2

symfony2 controller,symfony2

1、基本概念

一次http请求    输入(Request):header信息、get信息、post数据等

                     输出(Response):symfony经过处理返回的信息,包括页面、json字符串、URL跳转等

2、Request

$this->getRequest()

httpie工具

HTTPie (读aych-tee-tee-pie)是一个 HTTP 的命令行客户端。其目标是让 CLI 和 web 服务之间的交互尽可能的人性化。

安装参考http://blog.csdn.net/pzw_0612/article/details/46521965

http://www.cnblogs.com/huangjacky/archive/2012/03/28/2421866.html

用httpie模拟表单提交(post)

>http -f post http:<span>//</span><span>localhost:8000/app_dev.php/page/test name=lily</span>

3、Response

<?<span>php

namespace Scource\WebBundle\Controller;

</span><span>use</span><span> Symfony\Bundle\FrameworkBundle\Controller\Controller;
</span><span>use</span><span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;</span><strong><span>use</span><span> Symfony\Component\HttpFoundation\Response;//注意不要引用错
</span></strong><span>use</span><span> Symfony\Component\HttpFoundation\JsonResponse;
</span><span>use</span><span> Symfony\Component\HttpFoundation\RedirectResponse;

</span><span>class</span> DefaultController <span>extends</span><span> Controller
{
    </span><span>/*</span><span>*
     * @Route("/page/test1")
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> test1Action(){<br />     <span>//不使用模板,直接输出内容或者跳转
        </span></span><span>//</span><span>return new RedirectResponse('http://www.cnblogs.com/tianxintian22/');重定向
        //return new JsonResponse(array('a'=>'abcdef'));返回json串</span>
        <span>return</span> <span>new</span> Response('11111111111'<span>);
    }
}</span>

 4、session

<span>$this</span>->getRequest()->getSession()->set('b', 'ni hao!'<span>);
</span><span>$this</span>->getRequest()->getSession()->get('b');

如果不能正确获取到session的值,可能是app/cache/dev目录下session的权限不对。

<span>//调用</span><span>flashmessage,只能显示一次就被抛弃,一般用在表单用户信息提示</span>
//php<br /><span>$this</span>->getRequest()->getSession()->getFlashBag()->add('notice','you have something wrong');<br /><br />//twig<br />{% for flashMessage in app.session.flashbag.get('notice') %}<br />    <div class="flash-notice"><br />        {{ flashMessage }}<br />    </div><br />{% endfor %}

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.