High-performance PHP framework Symfony2 classic getting started tutorial

Source: Internet
Author: User
This article mainly introduces the high-performance PHP framework Symfony2 getting started tutorial. It is of great learning value to those who want to learn Symfony2. For more information, see

This article mainly introduces the high-performance PHP framework Symfony2 getting started tutorial. It is of great learning value to those who want to learn Symfony2. For more information, see

Symfony2 is a PHP-based Web development framework with fast development speed and high performance. This article describes in detail the configuration and program development of the Symfony2 framework in the implementation process of a program example.

1. Download

First, download Symfony2 to or on this site. Take Ubuntu as an example. Use the. tgz compressed package, decompress the source file to the/var/www directory, and execute:

Tar zxvf Symfony_Standard_Vendors_2.0. ###. tgz-C/var/www

The above ### refers to the version number, which is BETA5.

After decompression, the Symfony2 directory is as follows:

/Var/www/<-Web root directory Symfony/<-Symfony2 extract directory app/<-directory for storing core symfony files cache/<-directory for storing cached files config/< -logs/<-src/<-application source code... vendor/<-supplier or third-party modules and plug-ins... web/<-Web entry app. php <-front-end controller in the production environment...

You can use:

Cd/var/www/Symfonyphp bin/vendors install

Ii. Configuration

The configuration of Symfony2 is very simple. You only need to enter:

Then follow the prompts to proceed. It is worth noting that the permissions of the app/cache and app/logs directories are problematic. Because I installed them in Ubuntu, they can be used (firehare is my username, you can use your user name here ):

# Rm-rf app/cache/* rm-rf app/logs/* # Set ACL sudo setfacl-R-m u: www-data: rwx-m u: firehare: rwx app/cache app/logs sudo setfacl-dR-m u: www-data: rwx-m u: firehare: rwx app/cache app/logs

If the system does not support the setfacl command, check the following two points:
If setfacl has been installed or not, run the following command to install it (in Ubuntu 11.10, the package is named acl ):

Sudo apt-get install setfacl

If setfacl has been installed, check the/etc/fstab file to see if acl options are added:

#/Var was on/dev/sda7 during installation UUID = c2cc4104-b421-479a-b21a-1108f8895110/var ext4 defaults, acl 0 2

Enter the database name and other information as prompted, and copy the information to the/var/www/Symfony/app/config/parameters. ini file, as shown below:

; These parameters can be imported into other config files; by enclosing the key with % (like % database_user %); Comments start with ';', as in php. ini [parameters] database_driver = "Courier" database_host = "localhost" database_name = "symfony" database_user = "symfony" database_password = "symfony" Login = "smtp" mailer_host = "localhost" mailer_user = "" mailer_password = "" locale = "zh_CN" secret = "29f96e9e70c2797cb77dd088d3954d3c38d9b33f"

If all are OK, you will get a Demo page when entering the following addresses in your browser:

3. program example:

1. Create a Bundle:

First, create a Bundle:

Php app/console gen: bundle "AcmeHelloBundle" src to ensure that the Acme namespace can be automatically loaded, please go to your app/autoload. add the following statement to the PHP file: $ loader-> registerNamespaces (array (//... // Add a custom namespace 'acme' => _ DIR __. '/.. /src ',//...)); finally, register the Bundle to Symfony2. Please go to your app/AppKernel. add the following statement to the PHP file: // app/AppKernel. php public function registerBundles () {$ bundles = array (//... new AcmeHelloBundleAcmeHelloBundle (),);//... return $ bundles ;}

2. Create a route

A route can be created in app/config/routing. yml, but in order to have good programming habits and code organization, you can put it in Resources/config/routing in the Bundle directory created. in yml, while in app/config/routing. in yml, only the reference of this route file is retained, as shown below:

# App/config/routing. yml homepage: pattern:/defaults: {_ controller: FrameworkBundle: Default: index} hello: resource: "@ AcmeHelloBundle/Resources/config/routing. yml"

The real routing is written in the src/Acme/HelloBundle/Resources/config/routing. yml routing File, as shown below:

# Src/Acme/HelloBundle/Resources/config/routing. yml hello: pattern:/hello/{name} ults: {_ controller: AcmeHelloBundle: Hello: index, name: 'put '}

3. Create a controller:

The Controller name must be HelloController. php, the reason is very simple, because your route has already given the Controller name. The controllers in line 4th and line 7th in the preceding route file both start with AcmeHelloBundle: Hello, acmeHelloBundle indicates the Bundle name, while Hello indicates the Controller name, so the controller must be HelloController. php and Controller naming conventions. The following indexes and say are the methods in the Controller class. The index method is defined below. Of course, the method named indexAction is also a naming convention:

// Src/Acme/HelloBundle/Controller/HelloController. php namespace AcmeHelloBundleController; use SymfonyComponentHttpFoundationResponse; class HelloController {public function indexAction ($ name) {return new Response ('Hello '. $ name .'!');}}

In this way, when we enter

Hello World! Such words.

4. Create a template:

To reuse the blocks in the layout file, you can use a template to replace the HTML statements in the controller. First, create a page layout file:

{# App/Resources/views/layout.html. twig #} {% Block title %} Hello Application {% endblock %} {% Block body %} {% endblock %}

Note: This file is located in the app/Resources/views/directory and is used as a global template file for the entire application. This file defines two blocks: title and body. Next, create a template dedicated to the Hello controller, as shown below:

{# Src/Acme/HelloBundle/Resources/views/Hello/index.html. twig #} {% extends ': layout.html. twig '% }{% block body %} Hello {name }}! {% Endblock %}

Related Article

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.