High-Performance PHP framework Symfony2 classic Getting Started Tutorial, symfony2 Getting Started Tutorial _ PHP Tutorial

Source: Internet
Author: User
High-Performance PHP framework Symfony2 classic Getting Started Tutorial, symfony2 Getting Started Tutorial. High-Performance PHP framework Symfony2 classic Getting Started Tutorial, symfony2 Getting Started Tutorial Symfony2 is a PHP-based Web development framework with fast development speed and high performance. This article takes a high-performance PHP framework Symfony2 classic Getting Started Tutorial, symfony2 Getting Started Tutorial

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

Download Symfony2 at http://symfony.com/download#download http://www.bkjia.com/codes/187833.html. 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:

http://localhost/Symfony/web/config.php

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="pdo_mysql"  database_host="localhost"  database_name="symfony"  database_user="symfony"  database_password="symfony"  mailer_transport="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:

http://localhost/Symfony/web/app_dev.php

3. program example:

1.Create 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}  defaults: { _controller: AcmeHelloBundle:Hello:index, name:'pu' }

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

http://localhost/hello/index/World

Hello World! Such words.

4.Create 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 %} 

In this file, it inherits the global template and defines the block body, so that it overwrites the body block in the global template. If the system overwrites the block body of the global template before rendering the template.
Finally, modify the HTML statement in the controller to render the above template:

// src/Acme/HelloBundle/Controller/HelloController.php namespace AcmeHelloBundleController; use SymfonyBundleFrameworkBundleControllerController; class HelloController extends Controller {  public function indexAction($name)  {   return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));  } }


The younger brother asked: Which php framework should be learned better than which cms for php beginners?

Now the mainstream uses Zhimeng and the Empire CMS
I personally prefer the Emperor CMS

For beginners of the php framework, we recommend that you use the integrated environment to install PHPNOW or APMServ with one click.
No need to make your computer too complicated

About getting started with the php Framework

Since it is a large project, we recommend that you do not use a framework. if you use zendframework or thinkphp, we also describe that smarty does not belong to the framework. we recommend that you use smarty as the template processing mechanism you developed.

Symfony2 is a PHP-based Web development framework. it features fast development speed and high performance. This article takes a process...

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.