步骤 9: 设置管理后台
设置管理后台¶
把将要举办的会议录入到数据库是项目管理员的工作。所谓的 管理后台 是网站中一个受保护的区域,用来让 项目管理员 管理网站数据,处理提交的反馈和做其它一些事。
我们如何快速做一个后台呢?通过用一个 bundle,它可以根据项目的数据模型来生成后台!EasyAdmin 最适合不过了。
配置 EasyAdmin¶
首先,把 EasyAdmin 加进项目的依赖中。
1 | $ symfony composer req "admin:^2"
|
它借助 Flex 的 recipe 生成了一个新的配置文件,可以用该文件来配置 EasyAdmin。
1 2 3 4 5 6 | #easy_admin:
# entities:
# # List the entity class name you want to manage
# - App\Entity\Product
# - App\Entity\Category
# - App\Entity\User
|
几乎所有安装包都有一个像这样的配置文件放在 config/packages/
目录下。大多数时候,精心选择的默认值在大部分项目里都能工作良好。
去掉前面几行的注释,并加上这个项目的模型类:
1 2 3 4 | easy_admin:
entities:
- App\Entity\Conference
- App\Entity\Comment
|
用 /admin
路径访问这个生成的后台。哇!有了一个漂亮且功能丰富的后台界面,可以管理会议和评论:

小技巧
为什么后台是通过 /admin
路径来访问的呢?那是 config/routes/easy_admin.yaml
文件里设置的默认前缀:
1 2 3 4 | easy_admin_bundle:
resource: '@EasyAdminBundle/Controller/EasyAdminController.php'
prefix: /admin
type: annotation
|
你可以把它改成你想要的任何路径。
现在还不能添加会议和评论到后台,不然的话你会看到一个 Object of class App\Entity\Conference could not be converted to string
的错误。EasyAdmin 尝试着显示与评论关联的会议,但是如果会议对象不能转换成字符串的话,显示就会失败。在 Conference
类里增加一个 __toString()
方法就可以修复这个问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | --- a/src/Entity/Conference.php
+++ b/src/Entity/Conference.php
@@ -44,6 +44,11 @@ class Conference
$this->comments = new ArrayCollection();
}
+ public function __toString(): string
+ {
+ return $this->city.' '.$this->year;
+ }
+
public function getId(): ?int
{
return $this->id;
|
对 Comment
类也做同样的处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | --- a/src/Entity/Comment.php
+++ b/src/Entity/Comment.php
@@ -48,6 +48,11 @@ class Comment
*/
private $photoFilename;
+ public function __toString(): string
+ {
+ return (string) $this->getEmail();
+ }
+
public function getId(): ?int
{
return $this->id;
|
现在你可以在管理后台里直接添加/修改/删除会议。你可以玩玩看,并且添加至少一个会议。

添加几个不带照片的评论。现在先手工设置下日期;在后面的步骤中,我们会让 createdAt
列自动填充。

定制 EasyAdmin¶
默认的后台运行得很好,但是我们可以在许多方面对它进行定制,以提高使用体验。让我们对它做一些简单的改变来展现定制的可能性。用下面的内容代替当前的配置:
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 | easy_admin:
site_name: Conference Guestbook
design:
menu:
- { route: 'homepage', label: 'Back to the website', icon: 'home' }
- { entity: 'Conference', label: 'Conferences', icon: 'map-marker' }
- { entity: 'Comment', label: 'Comments', icon: 'comments' }
entities:
Conference:
class: App\Entity\Conference
Comment:
class: App\Entity\Comment
list:
fields:
- author
- { property: 'email', type: 'email' }
- { property: 'createdAt', type: 'datetime' }
sort: ['createdAt', 'ASC']
filters: ['conference']
edit:
fields:
- { property: 'conference' }
- { property: 'createdAt', type: datetime, type_options: { disabled: true } }
- 'author'
- { property: 'email', type: 'email' }
- text
|
我们覆盖了 design
的那一部分配置,它在菜单项里加了一些图标,而且加了一个指向网站首页的链接。
对于 Comment
部分,列出这些字段可以让我们以任意顺序排列它们。我们对有一些字段做了微调,比如说创建日期被设为只读。filters
部分定义了在常规搜索框上面要显示哪些过滤条件。

这些改动只是对于 EasyAdmin 定制可能性的一个小小介绍。
在后台里玩玩,比如可以用会议来过滤评论,或者根据邮件来搜索评论。目前唯一的问题是任何人都可以进入后台。别担心,我们会在以后的步骤中将它保护起来。
1 | $ symfony run psql -c "TRUNCATE conference RESTART IDENTITY CASCADE"
|
- « Previous 步骤 8: 描述数据结构
- Next » 步骤 10: 构建用户界面
This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.