symfony请求

  1. 参考链接

     官网:
     https://symfony.com/doc/current/components/http_foundation.html
     中文:
     http://blog.csdn.net/ross1130/article/details/50056781
    
  2. GET

     use Symfony\Component\HttpFoundation\Request;
    
     $request = Request::createFromGlobals();
     $request->query->get('name');
    
  3. POST

     use Symfony\Component\HttpFoundation\Request;
    
     $request = Request::createFromGlobals();
     $request->request->get('name');
    
  4. 复杂请求

     $request = Request::createFromGlobals();
     $request_opt = $request->getContent();//相当于php://input
     unset($request);
    

symfony响应

  1. 常见响应头

     Access-Control-Max-Age: 预请求缓存时间,单位秒
     Access-Control-Expose-Headers: 允许的白名单Header
     Access-Control-Allow-Origin: 允许的域名
     Access-Control-Allow-Methods: 允许的请求类型
     Access-Control-Allow-Headers: 允许的自定义header
     Access-Control-Allow-Credentials: 是否允许带凭证的请求true或false
     # 注意
     Access-Control-Allow-Credentials=true时,Access-Control-Allow-Origin≠*
    
  2. 跳转

     return $this->redirect('http://www.sina.com');
     return $this->redirectToRoute('homepage');
     return new RedirectResponse($this->generateUrl('user_login'));
    
  3. 数据渲染界面render

     return $this->render(
         'default/index.html.twig',
         [
             'name'=>'dog',
             'age'=>20,
             'list'=>[6, 7, 8],
         ]
     );
     # 对应twig取数据用
     \{\{ name \}\}
     \{\{ age \}\}
    

请求、响应及路由

  1. 请求、响应及路由DEMO

     use Symfony\Component\HttpFoundation\Request;
     use Symfony\Component\HttpFoundation\Response;
     use Symfony\Bundle\FrameworkBundle\Controller\Controller;
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
     use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
    
     class BookChapterController extends Controller
     {
         /**
          * 路由名及别名↓
          * @Route("/route_name", name="route_name1")
          * 允许的请求方式↓
          * @Method({"GET", "POST"})
          */
         public function bookCatalog()
         {
             //接收请求
             $request = Request::createFromGlobals();
             $opt1 = $request->query->get('opt1');//GET
             $opt2 = $request->request->get('opt2');//POST
             unset($request);
    
             //获取数据
             $service = $this->get('service_name');
             $result = $service->serviceFunction($opt1, $opt2);
             unset($service);
    
             //消息响应
             $response = new Response();
             //设置响应状态
             $response->setStatusCode(Response::HTTP_OK);
             //设置缓存时长
             $response->headers->set('Access-Control-Max-Age', '1000');
             //设置允许的自定义header
             //$response->headers->set('Access-Control-Allow-Headers', '*');
             //设置允许的白名单Header
             //$response->headers->set('Access-Control-Expose-Headers', '*');
             //设置允许AJAX带凭证的请求(为TRUE时,Access-Control-Allow-Origin≠*)
             $response->headers->set('Access-Control-Allow-Credentials', 'TRUE');
             //设置允许AJAX请求跨域
             $response->headers->set('Access-Control-Allow-Origin', 'xxx.xxx.com');
             //设置允许AJAX请求方式
             $response->headers->set('Access-Control-Allow-Methods', 'GET, POST, HEAD');
             //设置返回数据内容
             $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
             //设置返回数据类型
             $response->headers->set('Content-Type', 'application/json;charset=utf-8');
             return $response;
         }
    
         //生成完整路由
         public function createUrl($route)
         {
             //ABSOLUTE_URL:完整url
             //NETWORK_PATH:相对url
             //ABSOLUTE_PATH:完整路径
             //RELATIVE_PATH:相对路径
             $url =  $this->generateUrl(
                 $route_name1,
                 $arr_option,
                 UrlGeneratorInterface::ABSOLUTE_URL
             );
             return $url;
         }
     }
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-08-14 11:26:37

results matching ""

    No results matching ""