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.
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
## above # refers to the version number, which was BETA5 when I downloaded it.
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 For vendor (third-party) content, you can use:
cd /var/www/Symfony php bin/vendors install
2. Configuration
The configuration of Symfony2 is very simple, you only need to configure it in the browser Enter:
http://localhost/Symfony/web/config.php
and then 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:
Whether setfacl has been installed, if not, you can install it through the following command (It seems to have been 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 /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 the information to /var/www/ Symfony/app/config/parameters.ini file, as follows:
; 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, enter the following in your browser When entering the address, you will get a Demo page:
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 a route
Routes can be created in app/config/routing.yml, but for With good programming habits and code organization, you can place it in Resources/config/routing.yml in the created Bundle directory, and only keep a reference to the routing file in app/config/routing.yml, as follows Shown:
# 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 name of the controller must be HelloController.php. The reason is very simple, because your routing has already The name of the controller is given. The controllers in lines 4 and 7 of the routing file above all start 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 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('<html><body>Hello '.$name.'!</body></html>'); } }
In this way, when we enter
http://localhost/hello/index/World
{# 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>
{# src/Acme/HelloBundle/Resources/views/Hello/index.html.twig #} {% extends '::layout.html.twig' %} {% block body %} Hello {{ name }}! {% endblock %}
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)); } }