Symfony2 development-Controller usage instance analysis, symfony2 instance analysis-PHP Tutorial

Source: Internet
Author: User
Symfony2 is developed based on instance analysis of Controller usage and symfony2 instance analysis. Symfony2 is used as an example to analyze the usage of the controller developed by symfony2. this document analyzes the usage of the controller developed by Symfony2. For your reference, we will share the following details: Analysis of Controller usage examples developed by the controller Symfony2, and analysis of symfony2 instances.

This article analyzes the use of the controller in Symfony2 development. We will share this with you for your reference. The details are as follows:

The controller is a PHP function. through this function, you can create task information based on HTTP requests and construct and return HTTP responses. Responses can be HTML pages, XML documents, serialized JSON arrays, images, redirection, 404 errors, or even everything you can think. The controller contains the abstract logic of the response that your application needs to create.

Receives the request and returns the basic lifecycle of the response.

1. each request is processed by a single front-end controller (such as app. php or index. php), and the front-end controller is responsible for guiding the framework;
2. View and match the request information and direct the route to a specific route. the route determines the controller to call;
3. execute the controller. the code in the controller creates and returns a Response object;
4. the content of the HTTP header and Response object will be sent back to the client.

Although the name is similar, the front-end controller is different from the controller we mentioned in this Chapter. the front-end controller is a PHP small file in your web directory, and all requests go through it directly. 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 can never edit, view, or worry about the front-end controller.

Write a simple controller

The previous article "quick start to ten minutes of Symfony learning" has explained how to create a Bundle. now we can directly explain how to add controllers. The controller is the infoAction method, which is affiliated with a controller class (UserController ). Don't be confused about the name: the controller class simply aggregates several controllers. Generally, the controller class will place multiple controllers (such as updateAction and deleteAction ).

// Symfony2 makes full use of the namespace function of PHP5.3 to import the class for the namespace ZM \ ApiBundle \ Controller of the entire Controller class namespace; // use the keyword import class, is the Controller must return // for convenience, Symfony2 provides a Controller base class to help implement some commonly used Controller tasks, your Controller class can access the required resources. By inheriting the class, you can use some of these methods. Use Symfony \ Bundle \ FrameworkBundle \ Controller; use Symfony \ Component \ HttpFoundation \ Response; use Symfony \ Component \ HttpFoundation \ Request; class UserController extends Controller {/*** view in the user's personal center * @ return Response */public function infoAction () {$ conn = $ this-> getDoctrine () -> getConnection (); $ request = Request: createFromGlobals ()-> request; $ phone = $ request-> get ('phone'); $ result = $ co Nn-> fetchAssoc ("SELECT * FROM user WHERE phone =? LIMIT 1 ", array ($ phone); // The controller is created and returns a Response object return new Response (json_encode ($ result), '123 ', array ('content-type' => 'application/json '));}}

Permanent Address: http://blog.it985.com/5916.html
This article is from the IT985 blog. please indicate the source and relevant links when reprinting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.