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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function