High performance PHP Framework Symfony2 Classic Getting Started tutorial, Symfony2 Getting Started Tutorial _php tutorial

Source: Internet
Author: User
Tags php framework

High performance PHP Framework Symfony2 Classic Introductory tutorial, Symfony2 Getting Started tutorial

Symfony2 is a PHP language-based Web development framework, with rapid development, high performance features. In this paper, the Configuration and program development of SYMFONY2 framework are described in detail with the implementation process of a program example.

First, download

The first is to download Symfony2, to http://symfony.com/download or the site download http://www.bkjia.com/codes/187833.html. I use the Ubuntu system as an example, using the. tgz compression package, unzip the source file into the/var/www directory and execute:

Tar zxvf symfony_standard_vendors_2.0.###.tgz-c/var/www

# # # #是指版本号 above, I'm BETA5 when I'm down.

After decompression, the Symfony2 directory is as follows:

/var/www/<-Web root symfony/<-Symfony2 Extract directory app/<-The directory of the core files Symfony cache/the   directory where the cache files are stored   config/<-Store The directory of the application global configuration   logs/<-The directory where the log is stored  src/<-application source code   ... vendor/<-Suppliers or third-party modules and plugins ...   web/<-Web portal   app.php <-front-end controller in the production environment   

If you need to install (if you downloaded the without vendor version) or update vendor (third party) content, you can use:

cd/var/www/symfonyphp bin/vendors Install

Second, the configuration

The configuration of the Symfony2 is simple and only needs to be entered in the browser:


Then follow the prompts to get it done. It is noteworthy that the App/cache and app/logs directory permissions problem, because I was installed under Ubuntu, so you can use (where Firehare is my user name, you can use your username here instead):

If the system does not support the SETFACL command, check 2 places:
Setfacl has been installed, if not, can be installed by the following command (in Ubuntu 11.10 as if it has been installed by default, the package is called ACL):

If Setfacl is already installed, then check the/etc/fstab file to see if the ACL option is added:

Then, according to the page prompts to fill in the database name and other information, and then copy the information into the/var/www/symfony/app/config/parameters.ini file, as follows:

; These parameters can be imported to 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"  

If all OK, you will get a demo page when you enter the following address in your browser:


Three, the procedure example:

1. Create bundles:

First create a bundle:

PHP app/console gen:bundle "Acmehellobundle" src to ensure that the Acme namespace can be automatically loaded, add the following statement in your app/autoload.php file: $loader Registernamespaces (Array (  //...//Add custom namespace  ' Acme ' = __dir__. /..   /src ',  //...)); Finally, to register the bundle with Symfony2, add the following statement in your app/appkernel.php file://app/appkernel.php Public Function registerbundles () {  $bundles = Array (   //...   New Acmehellobundleacmehellobundle (),  );   // ...   

2. Create a route

Routing can be created in app/config/routing.yml, but in order to have a good programming habit and code organization, you can put it in the resources/config/routing.yml of the bundle directory you built, and in app/config/ Only references to the route file are kept in Routing.yml, as follows:

# APP/CONFIG/ROUTING.YML Homepage:  pattern:/  defaults: {_controller:frameworkbundle:default:index} hello:  resource: "@AcmeHelloBundle/resources/config/routing.yml"

The real route is written in the SRC/ACME/HELLOBUNDLE/RESOURCES/CONFIG/ROUTING.YML routing file, as follows:

# src/acme/hellobundle/resources/config/routing.yml Hello:  pattern:/hello/{name}  defaults: {_controller: AcmeHelloBundle:Hello:index, Name: ' Pu '}

3. Create the controller:

The name of the controller must be hellocontroller.php, the reason is very simple, because you have the name of the controller has been given down, in the route file above the 4th and 7th line of the controller is Acmehellobundle:hello beginning, Where Acmehellobundle represents the bundle name, and Hello represents the controller name, so the controller must be the Hellocontroller.php,controller name prefix is the naming convention. The following index and say are the methods in the Controller class. The index method is defined below, of course the method named Indexaction is also named convention:

src/acme/hellobundle/controller/hellocontroller.php namespace Acmehellobundlecontroller; Use Symfonycomponenthttpfoundationresponse; Class Hellocontroller {public  function indexaction ($name)  {   return new Response (' Hello '. $name. '! ');  

So, when we enter in the browser


The words "Hello world!" will be displayed.

4. Create a template:

To be able to reuse chunks in a layout file, you can use templates instead of HTML statements in the controller. First create the page layout file:

{# App/resources/views/layout.html.twig #}       
     {% block title%} Hello application{% endblock%}       {% block body%} {% Endblock%}    

Note that the file is in the app/resources/views/directory and is scoped to the global template file for the entire application. Two blocks are defined in the file: Title and body. The next step is to create a template dedicated to the Hello controller, as follows:

{# Src/acme/hellobundle/resources/views/hello/index.html.twig #} {% extends ':: Layout.html.twig '%} {% block body%}  

In this file, it inherits the global template and defines the block body, which then overwrite the body block in the global template. If the system renders to the template, it will overwrite the block body of the global template and render it.
Finally, change 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));  } }

My little brother kneeling asked: for PHP beginners should learn which PHP framework and which CMS is better?

Now mainstream use weaving dream and Imperial CMS
I personally prefer Imperial CMS

PHP Framework Beginners recommend that you use the integrated environment with one click to install Phpnow or Apmserv
There's no need to complicate your own computer.

Things about getting started with the PHP framework

Since it is a large project, the proposal does not use the framework, if you can consider zendframework,thinkphp, while explaining that Smarty does not belong to the framework, it is recommended to use Smarty as a template processing mechanism you develop

http://www.bkjia.com/PHPjc/840749.html www.bkjia.com true http://www.bkjia.com/PHPjc/840749.html techarticle High performance PHP framework Symfony2 Classic Getting Started tutorial, Symfony2 Getting Started tutorial Symfony2 is a PHP-based web development framework, with rapid development, high performance features. 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.