{% raw %}

Symfony Flash 消息

原文: http://zetcode.com/symfony/flash/

Symfony Flash 消息教程展示了如何在 Symfony 中创建 Flash 消息。 Flash 消息是用于用户通知的临时消息。 它们存储在一个会话中,并且一旦检索就消失。

Symfony

Symfony 是一组可重用的 PHP 组件和一个用于 Web 项目的 PHP 框架。 Symfony 于 2005 年发布为免费软件。Symfony 的原始作者是 Fabien Potencier。 Symfony 受到 Spring 框架的极大启发。

Symfony Flash 示例

在下面的示例中,我们有一个简单的表单,其中有一个输入框用于输入用户名。 如果用户输入的名称无效(空或仅包含空格),则应用将在表单上方显示一个闪烁通知。

注意:在我们的应用中,我们有一个 GET 表单。 GET 方法被认为是安全,因此我们未实现 CSRF 保护。 Symfony CSRF 教程涵盖了 Symfony 中的 CSRF 保护。

  1. $ composer create-project symfony/skeleton flashmsg

使用composer,我们创建一个新的 Symfony 骨架项目。

  1. $ cd flashmsg

我们转到项目目录。

  1. $ composer require annotations twig

我们安装了两个包:annotationstwig

  1. $ composer require server maker --dev

我们安装了开发 Web 服务器和 Symfony maker

src/Service/Validate.php

  1. <?php
  2. namespace App\Service;
  3. class Validate
  4. {
  5. public function isValid(?string $name): bool
  6. {
  7. if (!isset($name) || trim($name) === '') {
  8. return false;
  9. } else {
  10. return true;
  11. }
  12. }
  13. }

Validate服务检查提供的字符串是否为空或仅包含空格。

注意:在生产应用中,我们使用一些验证库,例如 Symfony 的symfony/validator或 PHP Rackit 或 Respect。

  1. $ php bin/console make:controller FormController

创建了FormController

src/Controller/FormController.php

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use App\Service\Validate;
  8. class FormController extends AbstractController
  9. {
  10. /**
  11. * @Route("/", name="index")
  12. */
  13. public function index()
  14. {
  15. return $this->render('form/index.html.twig');
  16. }
  17. /**
  18. * @Route("/form", name="form")
  19. */
  20. public function doForm(Request $request, Validate $valService)
  21. {
  22. $name = $request->query->get("name");
  23. $validated = $valService->isValid($name);
  24. if ($validated) {
  25. $msg = sprintf("Hello %s!", $name);
  26. return new Response($msg, Response::HTTP_OK,
  27. ['content-type' => 'text/plain']);
  28. } else {
  29. $this->addFlash(
  30. 'notice', 'Invalid name entered'
  31. );
  32. return $this->redirectToRoute("index");
  33. }
  34. }
  35. }

FormController响应根路径和形式路径。

  1. /**
  2. * @Route("/", name="index")
  3. */
  4. public function index()
  5. {
  6. return $this->render('form/index.html.twig');
  7. }

根路径返回 HTML 表单。

  1. /**
  2. * @Route("/form", name="form")
  3. */
  4. public function doForm(Request $request, Validate $valService)
  5. {

doForm()方法中,我们注入了Request对象和Validate服务。

  1. $name = $request->get("name");
  2. $validated = $valService->isValid($name);

我们检索名称输入并对其进行验证。

  1. $this->addFlash(
  2. 'notice', 'Invalid name entered'
  3. );
  4. return $this->redirectToRoute("index");

如果输入无效,我们将添加带有addFlash()的 Flash 消息,并在index路径上添加确定。

templates/form/index.html.twig

  1. {% extends 'base.html.twig' %}
  2. {% block title %}Home page{% endblock %}
  3. {% block stylesheets %}
  4. <style> .flash-notice { color: red } </style>
  5. {% endblock %}
  6. {% block body %}
  7. {% for message in app.flashes('notice') %}
  8. <div class="flash-notice">
  9. {{ message }}
  10. </div>
  11. {% endfor %}
  12. <form action="/form">
  13. <div>
  14. <label>Enter your name:</label>
  15. <input type="text" name="name">
  16. </div>
  17. <button type="submit">Send</button>
  18. </form>
  19. {% endblock %}

FormController返回一个表单页面。 它包含用户名的输入。

  1. {% for message in app.flashes('notice') %}
  2. <div class="flash-notice">
  3. {{ message }}
  4. </div>
  5. {% endfor %}

当应用重定向到此页面时,我们浏览 Flash 消息并将其显示在表单上方的div标签中。

templates/base.html.twig

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>{% block title %}Welcome!{% endblock %}</title>
  6. {% block stylesheets %}{% endblock %}
  7. </head>
  8. <body>
  9. {% block body %}{% endblock %}
  10. </body>
  11. </html>

base.html.twig模板包含其他模板文件共享的代码。 它定义了将在子模板中替换的块。

  1. $ php bin/console server:run

我们运行该应用。

在本教程中,我们在 Symfony 中处理了 Flash 消息。

您可能也对以下相关教程感兴趣: Symfony 简介Symfony 验证教程Symfony 服务教程Symfony 表单教程PHP 教程或列出所有 Symfony 教程

{% endraw %}