High Performance PHP Framework Symfony2 Classic Introduction Tutorial

  • 2021-07-07 06:47:02
  • OfStack

Symfony2 is an Web development framework based on PHP language, which has the characteristics of fast development speed and high performance. In this paper, the configuration and program development of Symfony2 framework are described in detail with the implementation process of a program example.

Step 1 Download

First, download Symfony2, go to http://symfony.com/download or download https://www.ofstack.com/codes/187833. html on this site. I take the Ubuntu system as an example, using the compressed package of. tgz, decompressing the source file to the/var/www directory and executing:


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

The # # # above refers to the version number, and it was BETA5 when I downloaded it.

When unzipped, the directory of Symfony2 is as follows:


/var/www/ <- Web Root directory  
 Symfony/ <- Symfony2 Extract directory  
  app/ <-  Storage symfony Directory of core files of 
   cache/ <-  Directory where cache files are stored 
   config/ <-  The directory where the global configuration of the application is stored 
   logs/ <-  Directory where logs are stored 
  src/ <-  Application source code 
   ... 
  vendor/ <-  Supplier or 3 Modules and plug-ins of square 
   ... 
  web/ <- Web Entrance 
   app.php <-  Front-end controller in production environment 
   ... 

If you need to install (if you download 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 need to enter in the browser:


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

Then follow the prompts. Among them, it is worth noting that the permissions of app/cache and app/logs directories can be used because I installed under Ubuntu (where firehare is my username, you can use your username instead here):


# To be on the safe side  
rm -rf app/cache/* 
rm -rf app/logs/* 
# Settings 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 is installed or not, if not, you can install it with 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 is already installed, 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 file /var/www/Symfony/app/config/parameters. ini, 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 all OK, you will get 1 Demo page when you enter the following address in your browser:


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

3. Example program:

1. Create Bundle:

Create an Bundle first:


php app/console gen:bundle "AcmeHelloBundle" src
 To ensure that Acme Namespace can be loaded automatically, please click on your app/autoload.php Add the following statement to the file: 
$loader->registerNamespaces(array( 
 // ...
 // Add a custom namespace  
 'Acme' => __DIR__.'/../src', 
 // ... 
)); 
 Finally, the Bundle Register to Symfony2 In your app/AppKernel.php Add the following statement to the file: 
// app/AppKernel.php 
public function registerBundles() 
{ 
 $bundles = array( 
  // ... 
  new AcmeHelloBundleAcmeHelloBundle(), 
 ); 
 
 // ... 
 
 return $bundles; 
} 

2. Create a route

The route can be created in app/config/routing. yml, but for good programming habits and code organization, it can be placed in Resources/config/routing. yml in the Bundle directory that you build, and only the reference to the route file is kept in app/config/routing. yml, as follows:


/var/www/ <- Web Root directory  
 Symfony/ <- Symfony2 Extract directory  
  app/ <-  Storage symfony Directory of core files of 
   cache/ <-  Directory where cache files are stored 
   config/ <-  The directory where the global configuration of the application is stored 
   logs/ <-  Directory where logs are stored 
  src/ <-  Application source code 
   ... 
  vendor/ <-  Supplier or 3 Modules and plug-ins of square 
   ... 
  web/ <- Web Entrance 
   app.php <-  Front-end controller in production environment 
   ... 

0

The real route is written in the src/Acme/HelloBundle/Resources/config/routing. yml routing file as follows:


/var/www/ <- Web Root directory  
 Symfony/ <- Symfony2 Extract directory  
  app/ <-  Storage symfony Directory of core files of 
   cache/ <-  Directory where cache files are stored 
   config/ <-  The directory where the global configuration of the application is stored 
   logs/ <-  Directory where logs are stored 
  src/ <-  Application source code 
   ... 
  vendor/ <-  Supplier or 3 Modules and plug-ins of square 
   ... 
  web/ <- Web Entrance 
   app.php <-  Front-end controller in production environment 
   ... 

1

3. Create a controller:

The controller name 1 is HelloController. php, for the simple reason that you have already given the controller name, and the controllers in lines 4 and 7 of the routing file above start with AcmeHelloBundle: Hello, where AcmeHelloBundle means Bundle name and Hello means controller name, so the controller must be HelloController. php, and the Controller name fix is a naming convention. The following index and say are methods in the controller class. The index method is defined below, although the method name is indexAction, which is also a naming convention:


/var/www/ <- Web Root directory  
 Symfony/ <- Symfony2 Extract directory  
  app/ <-  Storage symfony Directory of core files of 
   cache/ <-  Directory where cache files are stored 
   config/ <-  The directory where the global configuration of the application is stored 
   logs/ <-  Directory where logs are stored 
  src/ <-  Application source code 
   ... 
  vendor/ <-  Supplier or 3 Modules and plug-ins of square 
   ... 
  web/ <- Web Entrance 
   app.php <-  Front-end controller in production environment 
   ... 

2

In this way, when we enter in the browser,


http://localhost/hello/index/World

Hello World! Words like this.

4. Create a template:

To be able to reuse blocks in the layout file, templates can be used instead of the HTML statement in the controller. Start by creating a page layout file:


/var/www/ <- Web Root directory  
 Symfony/ <- Symfony2 Extract directory  
  app/ <-  Storage symfony Directory of core files of 
   cache/ <-  Directory where cache files are stored 
   config/ <-  The directory where the global configuration of the application is stored 
   logs/ <-  Directory where logs are stored 
  src/ <-  Application source code 
   ... 
  vendor/ <-  Supplier or 3 Modules and plug-ins of square 
   ... 
  web/ <- Web Entrance 
   app.php <-  Front-end controller in production environment 
   ... 

4

Note that this file is located in the app/Resources/views/ directory, and the scope is the global template file for 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 follows:


/var/www/ <- Web Root directory  
 Symfony/ <- Symfony2 Extract directory  
  app/ <-  Storage symfony Directory of core files of 
   cache/ <-  Directory where cache files are stored 
   config/ <-  The directory where the global configuration of the application is stored 
   logs/ <-  Directory where logs are stored 
  src/ <-  Application source code 
   ... 
  vendor/ <-  Supplier or 3 Modules and plug-ins of square 
   ... 
  web/ <- Web Entrance 
   app.php <-  Front-end controller in production environment 
   ... 

5

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 is rendering to the template, the block body will overwrite the block body of the global template before rendering.
Finally, change the HTML statement in the controller to render the above template:


/var/www/ <- Web Root directory  
 Symfony/ <- Symfony2 Extract directory  
  app/ <-  Storage symfony Directory of core files of 
   cache/ <-  Directory where cache files are stored 
   config/ <-  The directory where the global configuration of the application is stored 
   logs/ <-  Directory where logs are stored 
  src/ <-  Application source code 
   ... 
  vendor/ <-  Supplier or 3 Modules and plug-ins of square 
   ... 
  web/ <- Web Entrance 
   app.php <-  Front-end controller in production environment 
   ... 

6

Related articles: