Home  >  Article  >  Backend Development  >  Analysis of controller usage examples developed by Symfony2

Analysis of controller usage examples developed by Symfony2

WBOY
WBOYOriginal
2016-07-29 09:03:54770browse

This article analyzes the usage of controllers developed with Symfony2 through examples. Share it with everyone for your reference, the details are as follows:

The controller is a PHP function, through which you can create task information based on HTTP requests, and build and return HTTP responses. Responses can be HTML pages, XML documents, serialized JSON arrays, images, redirects, 404 errors or even anything you can think of. Controllers contain the abstract logic your application needs to create responses.

Receive the request and return the basic life cycle of the response

1. Each request is processed by a single front-end controller (such as app.php or index.php) file, and the front-end controller is responsible for guiding the framework;
2. Route viewing and Match the request information and point it to a specific route, which determines which controller to call;
3. Execute the controller, and the code in the controller will create and return a Response object;
4. HTTP headers and Response objects The content will be sent back to the client.

Although the names are similar, the front-end controller is different from the controller we are talking about in this chapter. The front-end controller is a small PHP file in your web directory, and all requests go directly through it. A typical application will have a front-end controller for production (such as app.php) and a front-end controller for development (such as app_dev.php). You never have to edit, view, or worry about a front controller.

Writing a simple controller

The previous article "The Classic Tutorial for Learning Symfony in Ten Minutes" has already talked about how to create a Bundle. Now let's talk about how to add a controller. The controller is the infoAction method, which belongs to a controller class (UserController). Don't be confused by the name: a controller class simply groups several controllers together. Typically, a controller class will place multiple controllers (such as updateAction, deleteAction, etc.).

//Symfony2充分利用了PHP5.3的名称空间的功能去为整个控制器类命名空间
namespace ZM\ApiBundle\Controller;
//use关键字导入类,是控制器必须返回的
//出于方便的考虑,Symfony2提供了一个Controller基类,以帮助实现常用的一些控制器任务,你的控制器类能够访问所需的资源。通过继承该类,你可以利用其中的一些方法。
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
class UserController extends Controller {
  /**
   * 用户个人中心查看
   * @return Response
   */
  public function infoAction() {
    $conn = $this->getDoctrine()->getConnection();
    $request = Request::createFromGlobals()->request;
    $phone = $request->get('phone');
    $result = $conn->fetchAssoc("SELECT * FROM user WHERE phone = ? LIMIT 1", array($phone));
    //控制器创建并返回一个Response对象
    return new Response(json_encode($result), '200', array('Content-Type' => 'application/json'));
  }
}

The permanent address of this article: http://blog.it985.com/5916.html
This article comes from IT985 Blog. Please indicate the source and corresponding link when reprinting.

Readers who are interested in more content related to PHP framework can check out the special topics of this site: "Summary of Excellent PHP Development Framework", "Introduction Tutorial on Codeigniter", "Advanced Tutorial on CI (CodeIgniter) Framework", "Introduction and Common Use of Yii Framework" Summary of Skills" and "Introduction to ThinkPHP Tutorial"

I hope this article will be helpful to everyone's PHP programming based on the Symfony framework.

The above introduces the analysis of controller usage examples developed by Symfony2, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn