Home > php教程 > PHP开发 > High-performance PHP framework Symfony2 classic introductory tutorial

High-performance PHP framework Symfony2 classic introductory tutorial

高洛峰
Release: 2016-12-26 11:27:07
Original
1577 people have browsed it

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
Copy after login

## 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 <- 生产环境下的前端控制器
   ...
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 &#39;;&#39;, 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"
Copy after login


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
Copy after login

3. Program example:

1. Create Bundle:

First create a Bundle:

php app/console gen:bundle "AcmeHelloBundle" src
  为了确保Acme名称空间可以被自动加载,请在你的app/autoload.php文件添加下列语句:
$loader->registerNamespaces(array(
 // ...
 //添加自定义的名称空间
 &#39;Acme&#39; => __DIR__.&#39;/../src&#39;,
 // ...
));
  最后是将该Bundle注册到Symfony2中,请在你的app/AppKernel.php文件中添加下列语句:
// app/AppKernel.php
public function registerBundles()
{
 $bundles = array(
  // ...
  new AcmeHelloBundleAcmeHelloBundle(),
 );
  
 // ...
  
 return $bundles;
}
Copy after login

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"
Copy after login

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:&#39;pu&#39; }
Copy after login

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(&#39;<html><body>Hello &#39;.$name.&#39;!</body></html>&#39;);
 }
}
Copy after login

In this way, when we enter

http://localhost/hello/index/World
Copy after login
* in the browser &*

will display the words Hello World!.

4. Create a template:

In order to reuse blocks in the layout file, you can use templates to replace HTML statements in the controller. 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>
Copy after login

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 &#39;::layout.html.twig&#39; %}
{% block body %}
 Hello {{ name }}!
{% endblock %}
Copy after login

In this file, it inherits the global template and defines 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(&#39;AcmeHelloBundle:Hello:index.html.twig&#39;, array(&#39;name&#39; => $name));
 }
}
Copy after login

More related high-performance PHP framework Symfony2 classic introductory tutorials Please pay attention to the PHP Chinese website for articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template