Symfony2 development-controller usage instance analysis-php instance

Source: Internet
Author: User
This article mainly introduces the use of the Symfony2 controller, and analyzes the simple use skills of the Symfony2 controller based on the instance form. If you need it, you can refer to the example in this article to analyze 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.