Symfony是一款PHP開發框架,目前最新版本是4.0,長期支持版是3.4。本文記錄如何在CentOS 7中安裝、配置Symfony 4。

Symfony官方文檔頁 https://symfony.com/doc/current/index.html

操作系統信息

Info Details
OS Version CentOS Linux release 7.4.1708 (Core)
Kernel Version 3.10.0-693.21.1.el7.x86_64

已啓用SELinuxFirewalld防火牆

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[[email protected] ~]$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

[[email protected] ~]$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources:
  services: dhcpv6-client
  ports: 22/tcp 80/tcp 443/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks: echo-request echo-reply timestamp-reply timestamp-request
  rich rules:

[[email protected] ~]$

Requirements

Symfony 4.0

Symfony 4.0 requires PHP 7.1.3 or higher to run, in addition to other minor requirements. – Requirements for Running Symfony

Symfony application

To create your new Symfony application, first make sure you’re using PHP 7.1 or higher and have Composer installed. – Installing & Setting up the Symfony Framework

Composer

Composer requires PHP 5.3.2+ to run. A few sensitive php settings and compile flags are also required, but when using the installer you will be warned about any incompatibilities. – System Requirements

PHP Installation

LEMP開發環境的安裝、配置可參考本人Blog LEMP Stack Installation And Configuration On CentOS 7

版本信息

Info Details
Nginx 1.13.12
MySQL 5.7.21
PHP 7.2.4
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[[email protected] ~]$ nginx -v
nginx version: nginx/1.13.12

[[email protected] ~]$ mysql --version
mysql  Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using  EditLine wrapper

[[email protected] ~]$ php --version
PHP 7.2.4 (cli) (built: Mar 27 2018 17:23:35) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.4, Copyright (c) 1999-2018, by Zend Technologies
[[email protected] ~]$

Composer Installation

安裝方案來自Composer官方文檔 Download Composer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Download the installer to the current directory
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

# Verify the installer SHA-384
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

# Run the installer
php composer-setup.php

# Remove the installer
php -r "unlink('composer-setup.php');"

# move composer.phar to executed path
sudo mv composer.phar /usr/local/bin/composer
sudo chown root:root /usr/local/bin/composer

版本信息

Info Details
Composer 1.6.4
1
2
3
4
5
[[email protected] ~]$ composer -V
Composer version 1.6.4 2018-04-13 12:04:24
[[email protected] ~]$ composer --version
Composer version 1.6.4 2018-04-13 12:04:24
[[email protected] ~]$

Symfony

Symfony官方文檔 Installing & Setting up the Symfony Framework

Symfony 4不需要單獨安裝Symfony,直接通過Composer創建項目。

New Symfony Project

此處假設項目名爲axdlog

1
2
3
4
5
6
# Create new project
composer create-project symfony/website-skeleton axdlog

# Checking for Security Vulnerabilities
cd axdlog
composer require sec-checker --dev

注意Composer需要啓用函數proc_openproc_get_status,相關設置在文件/etc/php.inidisable_functions中,執行如下命令即可啓用:

1
2
3
sudo sed -r -i '/disable_functions[[:space:]]*=/{[email protected](proc_open|proc_get_status),[email protected]@g;}' /etc/php.ini
# restart php-fpm service
sudo systemctl restart php-fpm

否則會出現報錯

proc_open

Failed to download symfony/website-skeleton from dist: The Process class relies on proc_open, which is not available on your PHP installation.

proc_get_status

Warning: proc_get_status() has been disabled for security reasons in phar:///usr/local/bin/composer/vendor/symfony/process/Process.php on line 1279

PHP Warning: proc_get_status() has been disabled for security reasons in phar:///usr/local/bin/composer/vendor/symfony/process/Process.php on line 1279

安裝完成後,出現提示信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Some files may have been created or updated to configure your new packages.
Please review, edit and commit them: these files are yours.


 What's next?


  * Run your application:
    1. Change to the project directory
    2. Execute the php -S 127.0.0.1:8000 -t public command;
    3. Browse to the http://localhost:8000/ URL.

       Quit the server with CTRL-C.
       Run composer require server --dev for a better web server.

  * Read the documentation at https://symfony.com/doc


 Database Configuration


  * Modify your DATABASE_URL config in .env

  * Configure the driver (mysql) and
    server_version (5.7) in config/packages/doctrine.yaml


 How to test?


  * Write test cases in the tests/ folder
  * Run php bin/phpunit

當前目錄創建了一個名爲axdlog的目錄

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
[[email protected] axdlog]$ echo $PWD
/home/maxdsre/axdlog
[[email protected] axdlog]$ tree -L 2
.
├── assets
├── bin
│   ├── console
│   └── phpunit
├── composer.json
├── composer.lock
├── config
│   ├── bundles.php
│   ├── packages
│   ├── routes
│   ├── routes.yaml
│   ├── services_test.yaml
│   └── services.yaml
├── package.json
├── phpunit.xml.dist
├── public
│   └── index.php
├── src
│   ├── Controller
│   ├── Entity
│   ├── Kernel.php
│   ├── Migrations
│   └── Repository
├── symfony.lock
├── templates
│   └── base.html.twig
├── tests
├── translations
├── var
│   ├── cache
│   └── log
├── vendor
│   ├── autoload.php
│   ├── bin
│   ├── composer
│   ├── doctrine
│   ├── easycorp
│   ├── egulias
│   ├── fig
│   ├── jdorn
│   ├── monolog
│   ├── nikic
│   ├── ocramius
│   ├── phpdocumentor
│   ├── psr
│   ├── sensio
│   ├── sensiolabs
│   ├── swiftmailer
│   ├── symfony
│   ├── twig
│   ├── webmozart
│   └── zendframework
└── webpack.config.js

37 directories, 16 files
[[email protected] axdlog]$

Existing Symfony Project

對於已存在的Symfony項目,參考 Installing an Existing Symfony Application 進行相關操作。

Running

1
2
3
4
5
6
# move into your new project and install the server
cd axdlog
composer require server --dev

# start the server
php bin/console server:run

操作過程如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
[[email protected] axdlog]$ composer require server --dev
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
Executing script cache:clear [OK]
Executing script assets:install --symlink --relative public [OK]
Executing script security-checker security:check [OK]

[[email protected] axdlog]$ php bin/console server:run

 [OK] Server listening on http://127.0.0.1:8000                                                         

 // Quit the server with CONTROL-C.                                                                     

PHP 7.2.4 Development Server started at Sat Apr 14 13:26:34 2018
Listening on http://127.0.0.1:8000
Document root is /home/maxdsre/axdlog/public
Press Ctrl-C to quit.

本地監聽了8000端口,通過SSH端口轉發,將遠程主機的8000端口轉發到本地8000端口。

瀏覽器中輸入127.0.0.1:8000/config.php 可檢測Symfony的應用程序配置情況。

打開127.0.0.1:8000,顯示如下

Symfony Profiler

Create First Page in Symfony

Symfony官方文檔 Create your First Page in Symfony

Route and Controller

推薦使用Annotation Routes,詳見Routing

操作分2步驟

  • Create a route
  • Create a controller

進入目錄axdlog

在目錄src/AppBundle/Controller創建測試文件LuckyController.php,寫入如下信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php
// src/Controller/LuckyController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class LuckyController
{
    public function number()
    {
        $number = mt_rand(0, 100);

        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

在文件config/routes.yaml中配置URL路由地址

1
2
3
4
5
6
# config/routes.yaml

# the "app_lucky_number" route name is not important yet
app_lucky_number:
    path: /lucky/number
    controller: App\Controller\LuckyController::number

瀏覽器中輸入 127.0.0.1:8000/lucky/number,出現如下信息

Annotation Routes

URL路由地址可通過config/routes.yaml配置,也可通過annotation實現

1
2
# install annotations package
composer require annotations

將文件LuckyController.php內容修改如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// src/Controller/LuckyController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class LuckyController
{
    /**
    * @Route("/lucky_number")
    */
    public function number()
    {
        $number = mt_rand(0, 100);

        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

註釋config/routes.yaml中的配置,瀏覽器中輸入 127.0.0.1:8000/lucky_number,出現如下信息

Rendering a Template

If you’re returning HTML from your controller, you’ll probably want to render a template. Fortunately, Symfony comes with Twig: a templating language that’s easy, powerful and actually quite fun.

1
2
3
4
cd axdlog

# install Twig
composer require twig

使用函數render()渲染template(模板),將文件LuckyController.php內容修改如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
// src/Controller/LuckyController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

// class LuckyController
class LuckyController extends Controller
{
    /**
    * @Route("/twig/lucky_number")
    */
    public function number()
    {
        $number = mt_rand(0, 100);

        return $this->render('lucky/number.html.twig', array(
            'number' => $number,
        ));
    }
}

模板文件存放在目錄templates/中,創建文件templates/lucky/number.html.twig,寫入如下內容

1
2
3
{# templates/lucky/number.html.twig #}

<h1>Your lucky number is {{ number }}</h1>

瀏覽器中輸入 127.0.0.1:8000/twig/lucky_number,出現如下信息

有關template的詳細說明參見官方文檔 Creating and Using Templates

Project Structure

directory explanation
config/ 配置文件存放路徑
src/ PHP代碼存放路徑
templates/ Twig模板文件
bin/ 可執行文件,如bin/console
var/ 緩存(var/cache/)和日誌(var/logs/)
vendor/ 第三方庫存放路徑
public/ 項目文檔路徑

Web Server Configuration

Symfony官方文檔 Configuring a Web Server

此處暫不討論。

Change Logs

  • 2016.08.23 21:06 Tue Asia/Shanghai
    • 初稿完成
  • 2018.04.12 22:57 Thu America/Boston
    • 內容更新,勘誤,遷移到新Blog