17. Contoller(2)
1.请求、控制器、响应的生命周期
2.Redirecting
/*** @Route("/index/number")*/public function indexAction(){return $this->redirectToRoute('test_url',array('url'=>2,'aaa'=>'bbb'),301); return $this->redirect('http://www.baidu.com');}
等价于下面:
public function indexAction(){return new RedirectResponse($this->generateUrl('test_url',array('url'=>2)));return new RedirectResponse('http://www.baidu.com');}
3.Rendering Templates
// renders app/Resources/views/lucky/number.html.twig
return $this->render('lucky/number.html.twig', array('name' => $name));// renders app/Resources/views/lottery/lucky/number.html.twig
return $this->render('lottery/lucky/number.html.twig', array('name' => $name
));
return $this->render('WwwFirstBundle:Default:index.html.twig');
4. 管理错误和404页面
public function indexAction(){$product = '';if (empty($product)) {throw $this->createNotFoundException('The product is empty');}return $this->render('WwwFirstBundle:Default:index.html.twig');}
也可以直接抛异常:throw new \Exception('hello,error');
在每个示例中,一个带格式的错误页被显示给最终用户,而一个全是错误的调试页会被显示给开发者(当在调试模式app_dev.php查看该页时 )
5. Request对象作为一个控制器参数
public function indexAction(Request $request){$product = '';if (empty($product)) {throw new \Exception('hello,error');}return $this->render('WwwFirstBundle:Default:index.html.twig');}
6.管理Session
public function indexAction(Request $request){$session = $request->getSession();$session->set('name','value');$a = $session->get('name');var_dump($a);die;return $this->render('WwwFirstBundle:Default:index.html.twig');}
7. Flash Message
你也可以在用户session中存储一些指定的消息,这个消息被称为“flash message”(消息条子)。
证据规定,flash消息只能够使用一次:当你取回它们的时候它们会自动的消失。
这种特性使得“flash”消息特别适合存储用户通知。
public function updateAction(Request $request)
{$form = $this->createForm(...);$form->handleRequest($request);if ($form->isValid()) {// do some sort of processing$this->addFlash('notice','Your changes were saved!');// $this->addFlash is equivalent to $this->get('session')->getFlashBag()->addreturn $this->redirectToRoute(...);}return $this->render(...);
}
在处理请求之后,控制器设置了一个名为notice的flash消息,然后重定向。名称(notice)并不重要 – 它就是一个确定消息的识别符。接下来是模板(或者是更好的,在你的基础布局模板),从session中读取每一条信息:
{% for flash_message in app.session.flashBag.get('notice') %}<div class="flash-notice">{{ flash_message }}</div>
{% endfor %}
通常使用的notice,warning和error作为不同类型提示信息的键,但是你可以使用任何你需要的键。
你可以使用peek()方法来获取消息,它可以让消息保存住.
public function indexAction(Request $request)
{$request->isXmlHttpRequest(); // is it an Ajax request?$request->getPreferredLanguage(array('en', 'fr'));// retrieve GET and POST variables respectively$request->query->get('page');$request->request->get('page');// retrieve SERVER variables$request->server->get('HTTP_HOST');// retrieves an instance of UploadedFile identified by foo$request->files->get('foo');// retrieve a COOKIE value$request->cookies->get('PHPSESSID');// retrieve an HTTP request header, with normalized, lowercase keys$request->headers->get('host');$request->headers->get('content_type');
}
8. JSON Helper
http://symfony.com/doc/current/controller.html
http://www.symfonychina.com/doc/current/routing.html
17. Contoller(2)相关推荐
- ROS-Industrial :Robot Contoller Layer
文章目录 1. 概述 2. Robot Contoller:ROBOT STATE 2.1 Socket 设置 2.2 ROBOT STATE数据内容: 2.2.1 Robot Status Data ...
- 【CV】吴恩达机器学习课程笔记第17章
本系列文章如果没有特殊说明,正文内容均解释的是文字上方的图片 机器学习 | Coursera 吴恩达机器学习系列课程_bilibili 目录 17 大规模机器学习 17-1 学习大数据集 17-2 随 ...
- 零起点学算法17——比较2个数大小
零起点学算法17--比较2个数大小 Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lld Description 输入2个整数, ...
- 北语计算机应用基础2,北语17春《计算机应用基础》练习2
北语17春<计算机应用基础>练习2 (4页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 14.90 积分 2017秋北语17春<计算机应 ...
- 机器学习入门(17)— 输入 4 维数据、基于 im2col 展开来实现卷积层
1. 输入 4 维数据 CNN 中各层间传递的数据是 4 维数据.所谓 4 维数据,比如数据的形状是(10, 1, 28, 28),则它对应 10 个高为 28.长为 28.通道为 1 的数据.用 P ...
- C语言程序设计 细节总结(第1-7章)
C语言细节总结 温故而知新,许久没看C语言,发现了一些从前没注意的小细节,总结下来~ 第1章C语言及C程序概述 1.C语言有34种运算符.32个关键字.9种控制语句 第2章数据类型.运算符与表达式 2 ...
- qt 显示加速_新能源 | 吉利星越ePro上市 补贴后17.58万起售 加速更快油耗更低
点击上方"蓝字"关注我们 新车速递 | 车市速报 吉利星越ePro上市 补贴后17.58万起 加速更快油耗更低 编辑 | 马 妍 排版 | 马 妍 「本文阅读时长约为3分钟 ...
- linux 6.6 最新 gblic,Centos6 升级glibc-2.17,解决Requires: libc.so.6(GLIBC_2.14)(64bit)错误解决方法(推荐)...
出现问题 在Centos6安装mysql 5.7.x 系列提示错误如下: [root@VM_0_5_centos mysql]# rpm -ivh mysql-community-libs-5.7.1 ...
- linux web高级编程,寒假学习 第16.17天 (linux 高级编程)
寒假学习 第16.17天 (linux 高级编程) 笔记 总结 一.进程的基本控制(进程的同步) 1.进程的常见控制函数 pause sleep/usleep atexit on_exit i ...
- 解决微信H5获取SDK授权报错提示errMsg: “config:fail,Error: 系统错误,错误码:63002,invalid signature [20200908 22:17:17][]“
如果常规检查都做过可以仔细看下微信开放文档 这个里面的报错原因 如果都不是那么极有可能是(恭喜你!看样子你的H5页面日活人数还蛮多[呲牙]) 获取腾讯微信平台access_token超过每日默认上限2 ...
最新文章
- 研究生如何走出“迷茫”?
- 刚刚引来985大学建校区,这座城市再牵手世界名校办学!
- ERROR MESSAGE: Invalid command line: Malformed walker argument: Could not find walker with name
- Linux 下 zip unzip压缩与解压
- 修改RAC VIP IP
- 【转载】开源且免费软件包分类列表
- django基础1--django安装与helloworld测试
- C# Task.FromResult的用法
- 微信统一下单需注意问题
- Raki的图论入门到进阶算法简单总结
- 重启路由器可以换IP吗
- 最强推荐:Android攒了一个月的面试题及解答,震撼来袭免费下载!
- 批量下载wsdl文件
- winpcap/npcap 提高抓包效率 发UDP包失败
- 用java实现简单绘图
- 【PE结构】由浅入深PE基础学习-菜鸟手动查询导出表、相对虚拟地址(RVA)与文件偏移地址转换(FOA)...
- 使用python爬取高德POI数据,并转换为WGS84经纬度坐标的点矢量
- CCPC-Wannafly Comet OJ 夏季欢乐赛(2019)A——完全k叉树
- HTML语言怎么对表格求和,excel求和怎么弄 EXCEL表格求和公式怎么运用?
- Python 技术篇-用mutagen库提取MP3歌曲图片实例演示