Detailed SYMFONY2 framework creating projects and setting up templates

Source: Internet
Author: User
This paper mainly introduces the method of Symfony2 framework to create project and template, and analyzes the concrete steps and detailed implementation code of SYMFONY2 framework in detail with instance form. Need friends can refer to, hope to help you.

Environment Preparation and overview

Accustomed to using the Netbean Editor in Windows and using the VirtualBox virtual CentOS system, pre-nginx+php-fpm+mysql, of course, Apache is also a good choice, using HTTP// Symfony is developed as a domain name on windows and CentOS.

First, download and environment settings

1. How to build a development environment on CentOS is no longer detailed, but you can also build a development environment on Windows.

2. About using Symfony instead of 127.0.0.1 to modify the/etc/hosts file in the Liunx system, modify the Win7 file in the C:\Windows\System32\drivers\etc\host system (need to open with Administrator privileges)

Add content similar to the IP alias 1 alias 2, such as:

#/etc/hosts 127.0.0.1 Symblog Dev symfony

3. Manually download the Symfony2.

The only thing to note: The App/cache and app/logs directories need to be set to 777 permissions. The Windows development environment should not exist for this issue.

4. Modify the Apache or nginx configuration file symfony domain name to the web directory of the downloaded Symfony file.

You should be able to access the default page of Symfony via http://symfony/app_dev.php, and there are several demos that can be referenced to learn.

app_dev.php A developer toolbar is loaded by default, which shows some information about the current page, which greatly facilitates debugging of the program, which is displayed only when the environment variable is dev.

5. When using composer installation, you will be prompted to output MySQL and other related information, need to modify this information, or directly download the file, you can go to the page "Configure" for the relevant settings.

Bundles (perhaps referred to as package, bundle, assembly, or project, or English bar) is the basis of symfony, one by one to share a reusable code package, even symfony itself as a Bundles run. including controllers, modules, templates, and even images and JS, CSS style sheets and other resources. Very messy things, different bundles use the php5.3 after the namespace, most cpenal, da virtual host as if only php5.2 version of it, can not run Symfony2.

Second, create a bundle

In the following example, a blog will be created, and Symfony provides a number of tools to quickly create a project. For example, we can use it to quickly create a blog base bundle.

PHP app/console generate:bundle–namespace=blogger/blogbundle–format=yml

After running, use all the default settings directly. It is easy to create the basic controller, modules and templates we need. The following behaviors are included:

Register Bundles

All bundles used in symfony are required to be registered first, and some bundles will only be used for development test environments (dev or test), as mentioned in the previous Development toolbar. The following code shows how the bundles creation command registers the Bloggerblogbundle bundle.

App/appkernel.phpclass Appkernel extends Kernel {public Function registerbundles () {$bundles = array (//). New Blogger\blogbundle\bloggerblogbundle (),);//.. return $bundles; } // ..}}

Routing

As a framework, the routing feature was created by the Bundler creator in App/config/routing.yml, Symfony is to save configuration information in YML format.

# App/config/routing.ymlbloggerblogbundle:resource: "@BloggerBlogBundle/resources/config/routing.yml" prefix:/

The prefix prefix option allows us to place it in sub-directories such as blogs, news, and so on.
File
In addition to the above configuration files, most of the other files are born into the SRC directory, like most MVC frameworks. The blogger directory is generated under SRC, and there is a blogbundle subdirectory that stores all kinds of related stuff. The difference is that the blogger-like directory corresponds to the PHP namespace.

Default Controller

The bundle generator generates the default controller under SRC. By visiting: Http://symfony/app_dev.php/hello/world can see a simple greeting. about how this page is generated:

Routing

or routing, the difference is that the previous route is registered in the entire program to use, where the route is to control the use of specific pages, SRC/BLOGGER/BLOGBUNDLE/RESOURCES/CONFIG/ROUTING.YML Controls the Bloggerblogbundle, which contains the following program fragments:

# Src/blogger/blogbundle/resources/config/routing.ymlbloggerblogbundle_homepage:pattern:/hello/{name}defaults: { _controller:bloggerblogbundle:default:index}

Parameters: URL detection, any value conforming to the/HELLO/{NAME} structure will be assigned to {name},
Way: There is no restriction on the form, the theory can put, get, post, delete all the operations can be done.
Subsequent: If the above two is met, then {name} will be transmitted to a specific file, the above is src/blogger/blogbundle/controller/ The index behavior of the default controller in the defaultcontroller.php file will be used.

Controller

In the default production bundler, the controller behaves fairly simply, and the {name} parameter is passed in and sent out to the template file:

Src/blogger/blogbundle/controller/defaultcontroller.phpnamespace Blogger\blogbundle\controller;use Symfony\ Bundle\frameworkbundle\controller\controller;class Defaultcontroller extends Controller{public function indexAction ($name) {return $this->render (' BloggerBlogBundle:Default:index.html.twig ', Array (' name ' = = $name)}}

BloggerBlogBundle:Default:index.html.twig will use the Bloggerblogbundle Views folder under the Default folder under the Index.html.twig template file.

Template file

Open the template file above, a very simple code:

{# Src/blogger/blogbundle/resources/views/default/index.html.twig #} Hello {{Name}}!

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.