zoukankan      html  css  js  c++  java
  • symfony2 controller

    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 ScourceWebBundleController;
    
    use SymfonyBundleFrameworkBundleControllerController;
    use SensioBundleFrameworkExtraBundleConfigurationRoute;
    use SymfonyComponentHttpFoundationResponse;//注意不要引用错 use SymfonyComponentHttpFoundationJsonResponse; use SymfonyComponentHttpFoundationRedirectResponse; 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') %}
        <div class="flash-notice">
            {{ flashMessage }}
        </div>
    {% endfor %}

    5、Service -- 服务

     程序架构的方式

    作用:把特定事情的类或方法独立于当前代码单独存在并通过Service进行调用。

    获取服务:$this->get();....

  • 相关阅读:
    C 语言中 static 的作用
    STM32 标准库
    STM32 HAL库与标准库的区别_浅谈句柄、MSP函数、Callback函数
    asp.net core launchsettings.json
    asp.net core mvc/api部署到iis
    依赖倒置来反转依赖
    ASP.NET Core in2020
    DDD学习一
    asp.net core学习一
    从零开始实现ASP.NET Core MVC的插件式开发
  • 原文地址:https://www.cnblogs.com/tianxintian22/p/5195958.html
Copyright © 2011-2022 走看看