Home  >  Article  >  Backend Development  >  Example analysis of controller usage in Symfony2 development, symfony2 example analysis_PHP tutorial

Example analysis of controller usage in Symfony2 development, symfony2 example analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:59:12671browse

Example analysis of controller usage developed by Symfony2, symfony2 example analysis

This article provides an example analysis of controller usage developed by Symfony2. Share it with everyone for your reference, the details are as follows:

A 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. The route checks and matches the request information and points 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. The contents of the HTTP header and Response object 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 controller for production (such as app.php) and a front controller for development (such as app_dev.php). You never have to edit, view, or worry about a front controller.

Write a simple controller

The previous article "The Classic Ten-Minute Tutorial for Learning Symfony" has already talked about how to create a Bundle and now we will directly 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 the PHP framework can check out the special topics of this site: "Summary of PHP Excellent Development Framework", "Introduction Tutorial on Codeigniter", "Advanced Tutorial on CI (CodeIgniter) Framework", "Introduction to Yii Framework and Summary of common techniques" and "ThinkPHP introductory tutorial"

I hope this article will be helpful to everyone’s PHP program design based on the Symfony framework.

Articles you may be interested in:

  • Symfony2’s method of implementing built-in data in doctrine
  • Detailed explanation of installing third-party Bundles instances in Symfony2
  • Symfony2 usage chapter Detailed explanation of the image upload example made by the third-party library Upload
  • Graphic tutorial on the configuration method of Symfony2 under Nginx
  • Symfony2 installation method (2 methods)
  • Symfony2 session usage example analysis
  • High-performance PHP framework Symfony2 classic introductory tutorial
  • Symfony learning ten-minute introductory classic tutorial
  • Symfony data verification method example analysis
  • symfony forms and pages Implementation skills

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099079.htmlTechArticleSymfony2 developed controller usage example analysis, symfony2 example analysis This article provides an example analysis of the controller usage developed by Symfony2. Share it with everyone for your reference, the details are as follows: Controller...
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