This article describes how to create an instance on the Symfony page. It analyzes the creation process and precautions of the Symfony page in detail. For more information, see
This article describes how to create an instance on the Symfony page. It analyzes the creation process and precautions of the Symfony page in detail. For more information, see
This document describes how to create a Symfony page. Share it with you for your reference. The details are as follows:
Here we will learn how to create a module, which is a structured element of the organizational page. At the same time, we will also learn how to create a page with one action and one template. The reason why the page is divided into action and template is the MVC mode. Link and commendation are basic page interactions. We will learn how to insert these elements in the template and process them in actions.
Create a module framework
Symfony organizes the page as a module. Before creating a page, we need to create a module and initialize it as an empty shell of a file structure that Symfony can recognize.
Symfony Command Line Automatic Processing Module Creation. We only need to call the init-module task and use the program name and module name as the parameter. After creating a myapp Program, to add a mymodule module to the program, run the following command:
The Code is as follows:
> Cd ~ /Myproject
> Symfony init-module myapp mymodule
> Dir ++ ~ /Myproject/apps/myapp/modules/mymodule
> Dir ++ ~ /Myproject/apps/myapp/modules/mymodule/actions
> File ++ ~ /Myproject/apps/myapp/modules/mymodule/actions. class. php
> Dir ++ ~ /Myproject/apps/myapp/modules/mymodule/config
> Dir ++ ~ /Myproject/apps/myapp/modules/mymodule/lib
> Dir ++ ~ /Myproject/apps/myapp/modules/mymodule/templates
> File ++ ~ /Myproject/apps/myapp/modules/mymodule/templates/indexSuccess. php
> Dir ++ ~ /Myproject/apps/myapp/modules/mymodule/validate
> File ++ ~ /Myproject/test/functional/myapp/mymoduleActionsTest. php
> Tokens ~ /Myproject/test/functional/myapp/mymoduleActionsTest. php
> Tokens ~ /Myproject/apps/myapp/modules/mymodule/actions. class. php
> Tokens ~ /Myproject/apps/myapp/modules/mymodule/templates/indexSuccess. php
It is separated from the actions/, config/, lib/, templates/, and validate/directories. This command creates only three files. One in the test/directory is a unit test. Actions. class. php points to the default module welcome page. The templates/indexSuccess. php file is empty.
The default action generated in the actions/actions. class. php file:
The Code is as follows:
<? Php
Class mymoduleActions extends sfActions
{
Public function executeIndex ()
{
$ This-> forward ('default', 'module ');
}
}
?>
For each new module, Symfony creates a default index action. It is composed of an action method named executeIndex and a template file named indexSuccess. php. We can use the following URL to browse the corresponding page:
The default index action is not used here, so we can remove the executeIndex () method from the actions. class. php file and delete the indexSuccess. php file from the templates/directory.
In addition to the command line, Symfony also provides other methods to initialize a module. One method is to manually create directories and files. In many cases, the actions and templates of a module mean to operate on the data of a given data table. Because the Code required to create, retrieve, update, and delete data records from a data table is usually the same, Symfony provides a mechanism named framework to generate the code. We will continue to introduce it later.
Add a page
In Symfony, the logic behind the page is stored in the action, while the surface is in the template. An empty action is still required for a page without logic.
Add an action
"Hello, world! "The page will be accessed through a myAction action. To create this action, you only need to add the executeMyAction method to the mymoduleActions class, as shown below:
The Code is as follows:
<? Php
Class mymoduleActions extends sfActions
{
Public function executeMyAction ()
{
}
}
The action method name is always in the form of execute 'xxx' (). The second part of the name is the action name, and the first letter is capitalized.
Now we can request the following URL:
Symfony will complain about the loss of the myActionSuccess. php template. This is normal. In Symfony, a page is usually composed of one action and one template.
URL is part of the response
Symfony contains a routing system that allows us to completely separate the actual action name from the URL format to be called. This allows you to customize the URL format, just as it is part of the response. We no longer limit the file structure or request parameters. The URL of an action looks like what we want to resolve. For example, calling an index action called by the article module is usually as follows:
? Id = 123
This URL is used to obtain a specified article from a piece of data. However, the URL can be written in a completely different way by making some minor changes in the routingyml configuration file:
Such a URL is not only friendly to search engines, but also very important to users. In this way, users can use the address bar as a pseudo code command to customize queries. For example:
+ France + euro
Symfony knows how to parse and generate URLs for users. The routing system automatically removes the requested parameter from a simple URL and makes it available for action. It also formats the hyperlinks contained in the response to make them look more concise. We will learn more about this feature in Chapter 9.
In short, this means that the way we name the action of a program should not be affected by calling their URL, but by the function of the action in the program. An action name explains the actual content of an action, and is usually a verb (such as show, list, and edit) in an infinitus format ). The action name can be completely invisible to end users, so you do not have to worry about using an explicit action name. We can effectively use code annotations to explain our function functions, so that the code can be read more.
Add a template