An example of the controller usage of SYMFONY2 development _php Example

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, specific 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, front-end controller is responsible for guiding the framework;
2. Route to view and match the request information and point it to a specific route, which determines which controller to call;
3, execution controller, the code in the controller will create and return a response object;
4, HTTP headers and the contents of the response object are sent back to the client.

Although the name is similar, but the front-end controller is different from the controller we described in this section, 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 (such as app.php) for production and a front-end controller for development (such as app_dev.php). You can never have to edit, view, and worry about a front-end controller.

Write a simple controller

The previous "Symfony learning 10 minutes Introductory classic" has already said how to create bundle now directly 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 names: the controller class simply centralizes several controllers together. Typically, the controller class places multiple controllers (such as Updateaction, Deleteaction, and so on).

Symfony2 fully utilizes the function of PHP5.3 namespace to namespace Zm\apibundle\controller for the whole controller class namespace
;
Use keyword to import class, is the controller must return
//For convenient consideration, 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 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's 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), ' A ', Array (' Content-type ' => ') application /json '));
  }


This article permanent address: http://blog.it985.com/5916.html
This article comes from IT985 blog, reprint, please indicate the source and corresponding link.

For more information about PHP framework interested readers can view the site topics: "PHP Excellent Development Framework Summary", "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "Yii framework Introduction and common skills Summary" and " thinkphp Getting Started Tutorial "

I hope this article will help you with the PHP program design based on Symfony framework.

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.