symfony2 controller,symfony2

原创
2016-06-13 08:46:02 582浏览

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://localhost:8000/app_dev.php/page/test name=lily

3、Response

php

namespace Scource\WebBundle\Controller;

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

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

4、session

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

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

//调用flashmessage,只能显示一次就被抛弃,一般用在表单用户信息提示
//php
$this->getRequest()->getSession()->getFlashBag()->add('notice','you have something wrong');

//twig
{% for flashMessage in app.session.flashbag.get('notice') %}

{{ flashMessage }}

{% endfor %}

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。