This article mainly introduces how to create a project and set a template in the Symfony2 framework, and analyzes in detail the steps and implementation code of the Symfony2 framework in the form of instances, for more information about how to create a project and template in the Symfony2 framework, see the example in this article. 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