Home > Article > Backend Development > High-performance PHP framework Symfony2 classic introductory tutorial, symfony2 introductory tutorial_PHP tutorial
Symfony2 is a web development framework based on PHP language, which has the characteristics of fast development speed and high performance. This article describes the configuration and program development of the Symfony2 framework in detail through the implementation process of a program example.
1. Download
First download Symfony2, go to http://symfony.com/download or download from this site http://www.bkjia.com/codes/187833.html. I take Ubuntu system as an example, use .tgz compression 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. When I downloaded it, it was BETA5.
After unzipping, the directory of Symfony2 is as follows:
/var/www/ <- Web根目录 Symfony/ <- Symfony2解压目录 app/ <- 存放symfony的核心文件的目录 cache/ <- 存放缓存文件的目录 config/ <- 存放应用程序全局配置的目录 logs/ <- 存放日志的目录 src/ <- 应用程序源代码 ... vendor/ <- 供应商或第三方的模组和插件 ... web/ <- Web入口 app.php <- 生产环境下的前端控制器 ...
If you need to install (if you downloaded the without vendor version) or update vendor (third-party) content, you can use:
cd /var/www/Symfony php bin/vendors install
2. Configuration
The configuration of Symfony2 is very simple, just enter in the browser:
http://localhost/Symfony/web/config.php
Then just follow the prompts. What is worth noting is the permission issue of the app/cache and app/logs directories. Since I installed it under Ubuntu, I can use it (firehare is my user name, you can replace it with your user name here):
#为了保险起见 rm -rf app/cache/* rm -rf app/logs/* #设置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, there are two places to check:
Is setfacl already installed? If not, you can install it through the following command (it seems to be installed by default in Ubuntu 11.10, and the package is called acl):
sudo apt-get install setfacl
If setfacl has been installed, please check the /etc/fstab file to see if the acl option has been added:
# /var was on /dev/sda7 during installation UUID=c2cc4104-b421-479a-b21a-1108f8895110 /var ext4 defaults,acl 0 2
Then fill in the database name and other information according to the page prompts, and then copy this 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 everything is OK, you will get a Demo page when you enter the following address 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 为了确保Acme名称空间可以被自动加载,请在你的app/autoload.php文件添加下列语句: $loader->registerNamespaces(array( // ... //添加自定义的名称空间 'Acme' => __DIR__.'/../src', // ... )); 最后是将该Bundle注册到Symfony2中,请在你的app/AppKernel.php文件中添加下列语句: // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new AcmeHelloBundleAcmeHelloBundle(), ); // ... return $bundles; }
2. Create route
Routing can be created in app/config/routing.yml, but in order to have good programming habits and code organization, it can be placed in Resources/config/routing.yml in the created Bundle directory, and in Only the reference to the routing file is retained in app/config/routing.yml, 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 controller:
The name of the controller must be HelloController.php. The reason is very simple, because your routing has already given the name of the controller. The controllers in lines 4 and 7 of the routing file above are both It starts with AcmeHelloBundle:Hello, where AcmeHelloBundle represents the Bundle name, and Hello represents the controller name, so the controller must be HelloController.php, and the Controller name extension is the naming convention. As for the subsequent index and say, they are methods in the controller class. The index method is defined below. Of course, the method name is indexAction, which is also a naming convention:
// src/Acme/HelloBundle/Controller/HelloController.php namespace AcmeHelloBundleController; use SymfonyComponentHttpFoundationResponse; class HelloController { public function indexAction($name) { return new Response('<html><body>Hello '.$name.'!</body></html>'); } }
In this way, when we enter
in the browserhttp://localhost/hello/index/World
The words Hello World! will be displayed.
4.Create template:
To be able to reuse blocks in layout files, templates can be used to replace HTML statements in controllers. First create the page layout file:
{# app/Resources/views/layout.html.twig #} <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>{% block title %}Hello Application{% endblock %}</title> </head> <body> {% block body %}{% endblock %} </body> </html>
Note that this file is located in the app/Resources/views/ directory, and its scope is the global template file of the entire application. Two blocks are defined in this file: title and body. The next step is to 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, thus overriding the body block in the global template. If the system renders this template, it will overwrite the block body of the global template with the block body and then 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)); } }
Nowadays, the mainstream use of Dreamweaver and Empire CMS
I personally prefer Empire CMS
Beginners of the PHP framework recommend that you use the integrated environment to install PHPNOW or APMServ with one click
There is no need to make your computer too complicated
Since it is a big project, it is recommended not to use a framework. If you use it, you can consider zendframework or thinkphp. At the same time, please note that smarty does not belong to the framework. It is recommended to use smarty as the template processing mechanism you develop