An example analysis of controller usage of SYMFONY2 development

Source: Internet
Author: User
Tags php framework codeigniter
In this paper, the controller usage of SYMFONY2 development is analyzed. Share to everyone for your reference, 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. The response can be an HTML page, an XML document, a serialized JSON array, a picture, a redirect, a 404 error, or even everything you can think of. The controller contains the abstract logic that your application needs to create a response.

Receives the request, returns the basic life cycle of the response

1, each request is a single front-end controller (such as app.php or index.php) file processing, the front controller is responsible for the boot framework;
2. The route views and matches the request information and points it to a specific route that determines which controller to call;
3, execute the controller, the code in the controller will create and return a response object;
4. The contents of the HTTP header and the response object are sent back to the client.

Although the name is similar, the front-end controller is different from what we have described in this section, the front controller is a small php file in your web directory, and all requests go directly through it. A typical application would have a front-end controller (such as app.php) for production and a front-end controller (such as app_dev.php) for development. You can never need to edit, view, and worry about the front controller.

Write a simple controller

The previous "Symfony learning 10-minute introductory classic" has already said how to create bundles now just say how to add a controller. The controller is the Infoaction method, which is subordinate to a controller class (Usercontroller). Don't be confused about the name: The controller class simply concentrates several controllers together. Typically, the controller class will place multiple controllers (such as Updateaction, Deleteaction, and so on).

Symfony2 takes full advantage of the function of the PHP5.3 namespace to import classes for the entire controller class namespace namespace Zm\apibundle\controller;//use keyword, is the controller must return//For convenience of consideration, SYMFONY2 provides a controller base class to help implement some of the commonly used controller tasks, and your controllers class has access to the required resources. By inheriting the class, you can take advantage of some of these methods. Use Symfony\bundle\frameworkbundle\controller\controller;use Symfony\component\httpfoundation\response;use Symfony\component\httpfoundation\request;class Usercontroller extends Controller {  /**   * User Personal Center View   * @ 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));    The controller creates and returns a Response object return    new Response (Json_encode ($result), ' $ ', array (' content-type ' = ' application ') /json '));}  }

Permanent address of this article: http://blog.it985.com/5916.html
This article is from IT985 blog, please indicate the source and corresponding link when reproduced.

Read more about PHP framework related content Readers can view this site: "PHP Excellent Development Framework Summary", "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "Yii framework Introduction and common skills Summary" and " thinkphp Getting Started Tutorial "

It is hoped that this article is helpful to the PHP program design based on Symfony framework.

The above describes the Symfony2 development of the Controller usage example analysis, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.