Details about how to create a project and a template setting instance in the Symfony2 framework

Source: Internet
Author: User
This example describes how to create a project and set a template in the Symfony2 framework. For your reference, the Environment preparation and overview are used to using the netbean editor in windows and the virtualbox virtual centos system, of course, apache is also a good choice, using symfony in wi

This example describes how to create a project and set a template in the Symfony2 framework. For your reference, the Environment preparation and overview are used to using the netbean editor in windows and the virtualbox virtual centos system, of course, apache is also a good choice, using http: // symfony in wi

This example describes how to create a project and set a template in the Symfony2 framework. We will share this with you for your reference. The details are as follows:

Environment preparation and overview

I used to use the netbean editor in windows and the virtualbox virtual centos system. I preinstalled nginx + php-fpm + mysql. Of course, apache is also a good choice, using http: // symfony serves as the development domain name on windows and centos.

1. Download and Environment Settings

1. How to create a development environment on centos is not described in detail. Of course, you can also create a development environment on windows.

2. modify the/etc/hosts file in liunx system instead of 127.0.0.1 using symfony, and modify the C: \ Windows \ System32 \ drivers \ etc \ host file in win7 system (which must be opened with administrator privileges)

Add content similar to IP alias 1 and alias 2, for example:

The Code is as follows:

#/Etc/hosts 127.0.0.1 symblog dev symfony

3. manually download symfony2. You can also refer to this page to use Composer for installation. Http://symfony.com/doc/current/book/installation.html

The only thing to note is that the app/cache and app/logs directories must be set to 777 permissions. This problem should not occur in the windows development environment.

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

In this case, you can access the default page of symfony through http: // symfony/app_dev.php. You can refer to several demos for more information.

App_dev.php loads a development toolbar under by default and displays some information on the current page, which greatly facilitates program debugging and is only displayed when the environment variable is dev.

5. When you use composer for installation, you will be prompted to output mysql and other related information. To modify the information or directly download the file, you can go to the "Configure" page to perform relevant settings.

Bundles (which can be called a package, bundle, assembly, or project) is the basic concept of symfony. It shares reusable code encapsulation one by one, even symfony runs as a bundles. Including controllers, modules, templates, images, js, css style sheets, and other resources. The difference between different bundles and namespaces after php5.3 is very messy. Most cpenal and da virtual hosts only have php5.2 and cannot run symfony2.

2. Create a Bundle

In the following example, we will create a blog. Symfony provides a large number of tools to quickly create a project. For example, we can use it to quickly create a basic bundle for a blog.

The Code is as follows:

Php app/console generate: bundle-namespace = Blogger/BlogBundle-format = yml

You can directly use all the default settings after running. You can easily create the required basic controllers, modules, and templates. Includes the following behaviors:

Register Bundles

All bundles used in symfony must be registered first. Some bundles will only be used in the development and testing environment (dev or test), as mentioned above. the broken code below shows how the bundles creation command registers the bundle of BloggerBlogBundle.

// app/AppKernel.phpclass AppKernel extends Kernel {public function registerBundles() {$bundles = array(// ..new Blogger\BlogBundle\BloggerBlogBundle(),);// .. return $bundles; } // ..}}

Routing

As a framework, the routing function is created by the bundler Creator in app/config/routing. yml, and symfony saves configuration information in yml format.

The Code is as follows:


# App/config/routing. yml
BloggerBlogBundle:
Resource: "@ BloggerBlogBundle/Resources/config/routing. yml"
Prefix :/

The prefix option allows us to place it in subdirectories such as blog and news.
File
In addition to the preceding configuration files, most other files are generated in the src directory, just like most mvc frameworks. The Blogger directory is generated under src, And the BlogBundle sub-directory stores various related things. The difference is that the directory similar to blogger corresponds to the php namespace.

Default Controller

The Bundle generator generates the default Controller under src. Visit: http: // symfony/app_dev.php/hello/world to see simple greetings. How to generate this page:

Routing

Or routing. The difference is that the previous routing is registered and used in the entire program. The routing here controls the use of specific pages, src/Blogger/BlogBundle/Resources/config/routing. yml controls BloggerBlogBundle and contains the following program fragments:

The Code is as follows:


# Src/Blogger/BlogBundle/Resources/config/routing. yml
BloggerBlogBundle_homepage:
Pattern:/hello/{name}
Defaults: {_ controller: BloggerBlogBundle: Default: index}

Parameter: Performs url detection. Any value conforming to the/hello/{name} structure will be assigned to {name },
Method: there is no restriction on the form. In theory, you can perform put, get, post, and delete operations.
Later: if the above two conditions are met, {name} will be transmitted to a specific file. The above conditions are src/Blogger/BlogBundle/Controller/DefaultController. the index behavior of the default controller in the PHP file will be used.

Controller

In the default production bundler, the Controller behavior is quite simple. The {name} parameter is passed in and passed 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 uses the Default file index.html. twig in the BloggerBlogBundle views folder.

Template File

Open the above template file with a very simple code:

{# src/Blogger/BlogBundle/Resources/views/Default/index.html.twig #}Hello {{ name }}!

The above is the entire mvc process of symfony. The role of so many files is only to output a "hello world". Theoretically, you do not need to create a bundler, but you can create the above files manually to achieve the same effect. It takes more time.

Back to the topic, we are creating a blog system, so we don't need hello world,

1. Remove the Controller src/Blogger/BlogBundle/Controller/DefaultController. php
2. Remove the src/Blogger/BlogBundle/Resources/views/Default/template/
3. Finally, remove the src/Blogger/BlogBundle/Resources/config/routing. yml route.
The whole world is quiet.

3. Let's start creating a blog homepage.

Advantages of Twig

In symfony, we can use Twig and php (this is not nonsense) as templates. Twig has the following advantages:

1. Fast: it is a compiled php class that can consume less resources.

2. Concise: Think about it. <? Php?>, There is much less Twig input.

3. Inheritance: a very cool Function

4. Security: the escape function is enabled by default and can even provide sandbox functions for important codes.

5. Scalability: Additional custom functions are required and can be expanded at any time.

For more information, see: http://twig.sensiolabs.org/

Inheritance is a very good advantage. We will use a three-level inheritance structure to customize this template, which will allow us to modify the template at three different levels for convenient customization.

Master template-level 1

 
 {% Block title %} symfony {% endblock %}-blog
 

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.