{% raw %}
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 保护。
$ composer create-project symfony/skeleton flashmsg
使用composer,我们创建一个新的 Symfony 骨架项目。
$ cd flashmsg
我们转到项目目录。
$ composer require annotations twig
我们安装了两个包:annotations和twig。
$ composer require server maker --dev
我们安装了开发 Web 服务器和 Symfony maker。
src/Service/Validate.php
<?phpnamespace App\Service;class Validate{public function isValid(?string $name): bool{if (!isset($name) || trim($name) === '') {return false;} else {return true;}}}
Validate服务检查提供的字符串是否为空或仅包含空格。
注意:在生产应用中,我们使用一些验证库,例如 Symfony 的
symfony/validator或 PHP Rackit 或 Respect。
$ php bin/console make:controller FormController
创建了FormController。
src/Controller/FormController.php
<?phpnamespace App\Controller;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use App\Service\Validate;class FormController extends AbstractController{/*** @Route("/", name="index")*/public function index(){return $this->render('form/index.html.twig');}/*** @Route("/form", name="form")*/public function doForm(Request $request, Validate $valService){$name = $request->query->get("name");$validated = $valService->isValid($name);if ($validated) {$msg = sprintf("Hello %s!", $name);return new Response($msg, Response::HTTP_OK,['content-type' => 'text/plain']);} else {$this->addFlash('notice', 'Invalid name entered');return $this->redirectToRoute("index");}}}
FormController响应根路径和形式路径。
/*** @Route("/", name="index")*/public function index(){return $this->render('form/index.html.twig');}
根路径返回 HTML 表单。
/*** @Route("/form", name="form")*/public function doForm(Request $request, Validate $valService){
在doForm()方法中,我们注入了Request对象和Validate服务。
$name = $request->get("name");$validated = $valService->isValid($name);
我们检索名称输入并对其进行验证。
$this->addFlash('notice', 'Invalid name entered');return $this->redirectToRoute("index");
如果输入无效,我们将添加带有addFlash()的 Flash 消息,并在index路径上添加确定。
templates/form/index.html.twig
{% extends 'base.html.twig' %}{% block title %}Home page{% endblock %}{% block stylesheets %}<style> .flash-notice { color: red } </style>{% endblock %}{% block body %}{% for message in app.flashes('notice') %}<div class="flash-notice">{{ message }}</div>{% endfor %}<form action="/form"><div><label>Enter your name:</label><input type="text" name="name"></div><button type="submit">Send</button></form>{% endblock %}
FormController返回一个表单页面。 它包含用户名的输入。
{% for message in app.flashes('notice') %}<div class="flash-notice">{{ message }}</div>{% endfor %}
当应用重定向到此页面时,我们浏览 Flash 消息并将其显示在表单上方的div标签中。
templates/base.html.twig
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>{% block title %}Welcome!{% endblock %}</title>{% block stylesheets %}{% endblock %}</head><body>{% block body %}{% endblock %}</body></html>
base.html.twig模板包含其他模板文件共享的代码。 它定义了将在子模板中替换的块。
$ php bin/console server:run
我们运行该应用。
在本教程中,我们在 Symfony 中处理了 Flash 消息。
您可能也对以下相关教程感兴趣: Symfony 简介, Symfony 验证教程, Symfony 服务教程, Symfony 表单教程 , PHP 教程或列出所有 Symfony 教程。
{% endraw %}
