Home >php教程 >php手册 >symfony2 controller,symfony2

symfony2 controller,symfony2

WBOY
WBOYOriginal
2016-06-13 08:46:02971browse

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