Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Controller in Symfony2

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use the controller in Symfony2, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The details are as follows:

A controller is a PHP function that you create that receives a HTTP request (request) and creates and returns an HTTP reply (Response). The reply object (Response) can be a HTML page, an XML document, a serialized JSON array, an image, a redirect, a 404 error, or whatever you want. Controller can contain any logic needed to render the content of your page.

Here is the simplest example of controller, just printing a Hello world!

Use Symfony\ Component\ HttpFoundation\ Response;public function helloAction () {return new Response ('Hello worldview');}

The ultimate goal of Controller is to create and return a Response object. In this way, you can read information from request objects, load database resources, send email, or write information to the user's Session. In all cases, however, Controller will eventually return a Response object and be distributed to the client.

For example, the following situations:

Controller A prepares a Response object to represent the homepage content of the site.

Controller B reads the slug parameter from Request to load a blog content from the database and create a Response object to display the blog. If slug does not exist in the database, it will create and return a Response object with a 404 status code.

Controller C handles a slave contact form, which reads the form information from the Request object, saves the contact information to the database and emails the administrator. Finally, it creates a Response object to redirect the client browser to the contact form thank you page.

Requests,Controller, the life cycle of Response

Every Request processed in the Symfony2 project goes through the same simple life cycle. The framework is responsible for repetitive tasks and eventually executes a controller that contains your application code:

1. Each Request is processed by a unified front-end controller file (for example, app.php, or app_dev.php), which starts the application.

2.Router reads the URI information from Request and finds the Route that matches it, and reads the _ controller parameter from that Route.

3. The controller that matches the successful route is executed, and the code in controller creates and returns a Response object.

The 4.HTTP header and the generated Response object content will be sent back to the client.

Creating a page is as easy as creating a controller. Create a route to map a URL to that controller.

Note: although the front controller is similar to controller in terms of name, they are actually different.

A front-end controller is a PHP file stored in the web directory, through which most Request is redirected. Each application has a product front-end controller app.php and a development front-end controller app_dev.php. You don't need to edit, view or worry about them.

Look at a simple Controller: any PHP callable content (such as a function, object method, or a Closure) can become a controller. In Symfongy2, a controller is usually a single method in a controller object. Controllers is also commonly referred to as actions.

/ / src/Acme/HelloBundle/Controller/HelloController.phpnamespace Acme\ HelloBundle\ Controller;use Symfony\ Component\ HttpFoundation\ Response;class HelloController {public function indexAction ($name) {return new Response ('Hello'. $name.names);}}

Note that in this example controller is the indexAction method, which exists in the controller class (HelloController). Don't be confused, a controller class (HelloController) is defined only to make it easier to organize multiple controllers/actions together. In general, a controller class will have multiple controllers/actions.

The controller in the above example is quite simple:

The Namespace line is that symfony2 uses the namespace functionality of PHP5.3 to specify a namespace for the entire controller class.

The use keyword imports the Response class, which is what our controller must return.

Controller class names are defined by adding Controller after their names, but only the first part is their real name. For the sake of unity, add Controller after it. Only the front part is taken when the route is configured.

Every method in the Controller class that is used for the real controller is appended with a uniform suffix Action, and we only take the first part and ignore Action when we configure its routing. Map it to some URL.

The end of each controller method is bound to create a Response object and return it.

Map a URL to a Controller method:

The controller method in the above example returns a simple HTML page. If you want to access the page in a browser, you need to create a route for it and map it to a specific mode of URL.

# app/config/routing.ymlhello: pattern: / hello/ {name} defaults: {_ controller: AcmeHelloBundle:Hello:index}

XML format:

AcmeHelloBundle:Hello:index

PHP code format:

/ / app/config/routing.php$collection- > add ('hello', new Route (' / hello/ {name}', array ('_ controller' = > 'AcmeHelloBundle:Hello:index',)

Now think that URL / hello/ryan will be mapped to HelloController::indexAction () controller and pass ryan to the $name variable.

To create a so-called page is to create a controller method and an associated route.

Notice that we use the representation syntax that points to the controller method: AcmeHelloBundle:Hello:index

Symfony2 uses a very flexible string declaration to point to different controller. It tells Symfony2 to find a class called HelloController in a bundle called AcmeHelloBundle and execute its indexAction () method. In this example, our routing configuration is written directly in the app/config/ directory, and a better way to organize it is to put your routes in their respective bundle.

Routing parameters as Controller method parameters

You have the _ controller parameter AcmeHelloBundle:Hello:index pointing to a method called HelloController::indexAction () located in AcmeHelloBundle. Interestingly, the parameters in the route are passed to this method.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report