请求环境(Request Environment)

每个 HTTP 请求(通常由浏览器发起),包含了请求参数、HTTP 头信息(包括Cookies)、文件等。Web 应用程序需要解析这些信息,并根据这些信息返回正确的响应给请求者。请求对象 Phalcon\Http\Request 封装了这些信息,你可以以面向对象的方式访问它。

  1. <?php
  2.  
  3. use Phalcon\Http\Request;
  4.  
  5. // Getting a request instance
  6. $request = new Request();
  7.  
  8. // Check whether the request was made with method POST
  9. if ($request->isPost()) {
  10. // Check whether the request was made with Ajax
  11. if ($request->isAjax()) {
  12. echo "Request was made using POST and AJAX";
  13. }
  14. }

获取请求参数(Getting Values)

PHP automatically fills the superglobal arrays $_GET and $_POST depending on the type of the request. These arrayscontain the values present in forms submitted or the parameters sent via the URL. The variables in the arrays arenever sanitized and can contain illegal characters or even malicious code, which can lead to SQL injection orCross Site Scripting (XSS) attacks.

Phalcon\Http\Request allows you to access the values stored in the $_REQUEST,$_GET and $_POST arrays and sanitize or filter them with the ‘filter’ service, (by defaultPhalcon\Filter). The following examples offer the same behavior:

  1. <?php
  2.  
  3. // Gets a variable from the $_REQUEST superglobal
  4. $id = $request->get("id");
  5.  
  6. // Checks whether $_REQUEST superglobal has certain index
  7. $ret = $request->has("id");
  8.  
  9. // Manually applying the filter
  10. $filter = new Phalcon\Filter();
  11. $email = $filter->sanitize($_POST["user_email"], "email");
  12.  
  13. // Manually applying the filter to the value
  14. $filter = new Phalcon\Filter();
  15. $email = $filter->sanitize($request->getPost("user_email"), "email");
  16.  
  17. // Automatically applying the filter
  18. $email = $request->getPost("user_email", "email");
  19.  
  20. // Setting a default value if the param is null
  21. $email = $request->getPost("user_email", "email", "some@example.com");
  22.  
  23. // Setting a default value if the param is null without filtering
  24. $email = $request->getPost("user_email", null, "some@example.com");
  25.  
  26. // Gets variable from the dispatcher
  27. $host = $request->getParam("id");
  28.  
  29. // Gets a variable from put request
  30. $id = $request->getPut("id");
  31.  
  32. // Gets variable from $_GET superglobal
  33. $id = $request->getQuery("id");
  34.  
  35. // Gets variable from $_SERVER superglobal
  36. $host = $request->getServer("HOST");

请求方法(Request Method)

判断请求方法(Check Method)

  1. <?php
  2.  
  3. // Check if HTTP method match any of the passed methods
  4. $ret = $request->isMethod('POST');
  5.  
  6. // Checks whether HTTP method is POST. if $_SERVER['REQUEST_METHOD']=='POST'
  7. $ret = $request->isPost();

获取请求方法(Getting Method)

  1. <?php
  2.  
  3. // Gets HTTP method which request has been made
  4. $id = $request->getMethod();

控制器中访问请求(Accessing the Request from Controllers)

The most common place to access the request environment is in an action of a controller. To access thePhalcon\Http\Request object from a controller you will need to usethe $this->request public property of the controller:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Controller;
  4.  
  5. class PostsController extends Controller
  6. {
  7. public function indexAction()
  8. {
  9.  
  10. }
  11.  
  12. public function saveAction()
  13. {
  14. // Check if request has made with POST
  15. if ($this->request->isPost()) {
  16.  
  17. // Access POST data
  18. $customerName = $this->request->getPost("name");
  19. $customerBorn = $this->request->getPost("born");
  20.  
  21. }
  22. }
  23. }

文件上传(Uploading Files)

Another common task is file uploading. Phalcon\Http\Request offersan object-oriented way to achieve this task:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Controller;
  4.  
  5. class PostsController extends Controller
  6. {
  7. public function uploadAction()
  8. {
  9. // Check if the user has uploaded files
  10. if ($this->request->hasFiles()) {
  11.  
  12. // Print the real file names and sizes
  13. foreach ($this->request->getUploadedFiles() as $file) {
  14.  
  15. // Print file details
  16. echo $file->getName(), " ", $file->getSize(), "\n";
  17.  
  18. // Move the file into the application
  19. $file->moveTo('files/' . $file->getName());
  20. }
  21. }
  22. }
  23. }

Each object returned by Phalcon\Http\Request::getUploadedFiles() is an instance of thePhalcon\Http\Request\File class. Using the $_FILES superglobalarray offers the same behavior. Phalcon\Http\Request\File encapsulatesonly the information related to each file uploaded with the request.

使用头信息(Working with Headers)

As mentioned above, request headers contain useful information that allow us to send the proper response back tothe user. The following examples show usages of that information:

  1. <?php
  2.  
  3. // Get the Http-X-Requested-With header
  4. $requestedWith = $request->getHeader("HTTP_X_REQUESTED_WITH");
  5. if ($requestedWith == "XMLHttpRequest") {
  6. echo "The request was made with Ajax";
  7. }
  8.  
  9. // Same as above
  10. if ($request->isAjax()) {
  11. echo "The request was made with Ajax";
  12. }
  13.  
  14. // Check the request layer
  15. if ($request->isSecureRequest()) {
  16. echo "The request was made using a secure layer";
  17. }
  18.  
  19. // Get the servers's IP address. ie. 192.168.0.100
  20. $ipAddress = $request->getServerAddress();
  21.  
  22. // Get the client's IP address ie. 201.245.53.51
  23. $ipAddress = $request->getClientAddress();
  24.  
  25. // Get the User Agent (HTTP_USER_AGENT)
  26. $userAgent = $request->getUserAgent();
  27.  
  28. // Get the best acceptable content by the browser. ie text/xml
  29. $contentType = $request->getAcceptableContent();
  30.  
  31. // Get the best charset accepted by the browser. ie. utf-8
  32. $charset = $request->getBestCharset();
  33.  
  34. // Get the best language accepted configured in the browser. ie. en-us
  35. $language = $request->getBestLanguage();

原文: http://www.myleftstudio.com/reference/request.html