上一篇文章我们介绍了 Symfony 4 带来的一些变化,我们尝试用一个 demo 示例说明一下。
2017-12-16 补充:此文内容为 Symfony 4 测试版用法,目前正式版本已经发布,用法与此文介绍还是有些小差别,比如正式版去掉 Make 文件的使用。请勿将此文当成教程使用。
首先是安装项目:
composer create-project symfony/skeleton:3.3.x-dev demo
cd demo
此命令只下载了一个骨架文件 composer.json
,并自动运行 composer install
命令。
Symfony Flex 是第一个下载的包。flex 安装完毕之后立马通过钩子程序勾住 composer 的安装过程。所以当 composer 开始安装其他组件包的时候,flex 通过与『食谱』服务器交互,并执行『食谱』里指定的操作。如果大家执行了上面的命令,一定会看到『食谱』执行的结果:
Installing symfony/skeleton (3.3.x-dev 19daacfd7ca93335963b7dd0e5ea4e18c228135c) - Installing symfony/skeleton (3.3.x-dev 19daacf): Cloning 19daacfd7c from cache Created project in demo Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 28 installs, 0 updates, 0 removals - Installing symfony/flex (dev-master 979058c): Downloading (100%) Detected auto-configuration settings for "symfony/flex" Setting configuration and copying files - Installing symfony/stopwatch (dev-master 602a152): Loading from cache - Installing symfony/polyfill-util (dev-master 746bce0): Loading from cache - Installing symfony/polyfill-php56 (dev-master 1dd42b9): Loading from cache - Installing paragonie/random_compat (v2.0.10): Loading from cache - Installing symfony/polyfill-php70 (dev-master 13ce343): Loading from cache - Installing symfony/security-core (dev-master 66aa40a): Loading from cache - Installing symfony/security-csrf (dev-master 307136b): Loading from cache - Installing symfony/routing (dev-master fadf939): Loading from cache Detected auto-configuration settings for "symfony/routing" - Installing symfony/polyfill-mbstring (dev-master e79d363): Loading from cache - Installing psr/log (dev-master 4ebe3a8): Loading from cache - Installing symfony/debug (dev-master 2a23722): Loading from cache - Installing symfony/http-foundation (dev-master af2d53e): Loading from cache - Installing symfony/event-dispatcher (dev-master a9f8b02): Downloading (100%) - Installing symfony/http-kernel (dev-master 669e819): Loading from cache - Installing symfony/finder (dev-master ce30991): Loading from cache - Installing symfony/filesystem (dev-master 57ecfd1): Loading from cache - Installing psr/container (dev-master b7ce3b1): Loading from cache - Installing symfony/dependency-injection (dev-master aa8e45d): Downloading (100%) - Installing symfony/config (dev-master a20ae35): Loading from cache - Installing symfony/class-loader (dev-master b0aff75): Loading from cache - Installing psr/simple-cache (dev-master 753fa59): Loading from cache - Installing psr/cache (dev-master 78c5a01): Loading from cache - Installing symfony/cache (dev-master a06e189): Loading from cache - Installing doctrine/cache (dev-master 0da649f): Loading from cache - Installing symfony/framework-bundle (dev-master 312a59d): Downloading (100%) Detected auto-configuration settings for "symfony/framework-bundle" Enabling the package as a Symfony bundle Setting configuration and copying files Adding environment variable defaults Adding Makefile entries Adding entries to .gitignore - Installing symfony/yaml (dev-master dcbd318): Loading from cache - Installing symfony/dotenv (dev-master 0f9d2e0): Loading from cache Writing lock file Generating autoload files Executing script make cache-warmup [OK] Skipping "assets:install --symlink --relative web" (needs symfony/console to run). What's next? * Run your application: 1. Change to the project directory 2. Execute the make serve command; 3. Browse to the http://localhost:8000/ URL. * Read the documentation at https://symfony.com/doc
你可以发现,composer install
命令输出的内容比没有使用 flex 的内容要丰富很多,比如告诉终端用户,已经自动注册 framework-bundle;将默认配置复制到项目中;将默认环境变量添加到环境中;添加 Makefile 的任务,更新了 .gitignore 里的内容……
另外还告诉用户,因为没有安装 symfony/console,所以无法执行 assets:install
命令。
既然已经提示需要安装 symfony/console,那么我们就来安装他,且慢,有了 flex,我们可以不用再老老实实用 composer require symfony/console
,因为 flex 给 symfony/console 设置了更简短的别名:
composer req cli
flex 给很多常用组件都设置了别名,比如 symfony/web-server-bundle:
composer req webserver
删除某个依赖的时候,也是可以使用别名的:
composer rem cli
你也可以删除 framework-bundle 试试,看看 flex 是如何在删除某个依赖组件后,自动删除相关的配置信息的
最后我们安装一个有意思的完整项目——EasyAdminBundle:
composer req admin
然后我们再在 src/Entity/
目录下,创建 Product.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", length=100)
*/
public $name;
/**
* @ORM\Column(type="decimal", scale=2)
*/
public $price;
/**
* @ORM\Column(type="text")
*/
public $description;
}
在 .env
文件可以调整一下数据库连接信息
###> doctrine/doctrine-bundle ###
DB_URL=mysql://root@127.0.0.1:3306/symfony?charset=utf8mb4
###< doctrine/doctrine-bundle ###
执行熟悉的创建数据库和表的命令
bin/console doctrine:database:create
bin/console doctrine:schema:update --force
最后到 etc/packages/easy_admin.yaml
添加要做后台管理的 Entity
easy_admin:
entities:
- App\Entity\Product
此时你可以通过熟悉的启动 web 服务命令来开启一个服务。如果你并不想安装 webserver,也可通过 make 来启动服务(注:正式版已经去掉这看似美好但其实有若干不好解决的问题最终被抛弃的功能)
make serve
# or
bin/console server:start
此时访问 http://localhost:8000
,就可以访问 admin 的页面了

此外你还可以尝试安装 mailer、profiler 等一大堆准备好的『食谱』,而这些都并不再默认包含在框架代码中,你可以按需安装了。
基于 Symfony 4 的开发示例 by Chris Yue is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

写作累,服务器还越来越贵
求分担,祝愿好人一生平安
天使打赏人
2 Comments
awhasuka
3月 17, 2019 在 6:27 下午老师,我是一个新手,现在在到处找教程学习。。现在基础我用的是sf4.2.4,网上也很少教程,学不到,内心有点绝望。所以我想问下。我是换下3.*的版本,按照老师你之前发布的教程学习好了呢。。。
Chris Yue
3月 18, 2019 在 3:33 下午如果你是新手,只是为了学习和掌握 SF 框架,我建议你换 3,因为 4 我觉得更亲近 SF 老手。
你不用担心学习 3 就好像错过了 4 的新东西,我保守估计 90% 的知识点都是一样的概念。直接学 4 我觉得有一个问题是,有一些新的概念,是为了解决或者优化 SF2~3 的,如果你直接上手 4,可能很多知识点你都不知道是解决了什么问题,会让你觉得莫名其妙,这是一种很不好的学习体验。
而当你把 3 掌握了之后,再来学 4,就会觉得很自然,会更容易发现并接受 4 的美妙之处。