Slim Flash Messages
v0.3.2
该库允许您在Slim项目中使用临时消息。通过提供用于抓取和使用模板中的功能的扩展,它可以轻松地与树枝模板系统集成在一起。它不仅限于创建简单的消息字符串,还可以使用其他数据类型(例如数组)。
composer require williamsampaio/slim-flash-messages // app/dependencies.php
//...
use SlimFlashMessages Flash ;
use SlimFlashMessages FlashProviderInterface ;
return function ( ContainerBuilder $ containerBuilder ) {
$ containerBuilder -> addDefinitions ([
//...
FlashProviderInterface::class => function () {
return Flash:: getInstance ();
},
]);
}; // app/middleware.php
//...
// use SlimFlashMessagesFlashMiddleware;
use SlimFlashMessages FlashTwigExtension ;
return function ( App $ app ) {
//...
// Optional if you are working with dependency injection,
// using the middleware is only useful if you need to obtain the Flash instance from request.
// $app->add(FlashMiddleware::createFromContainer($app));
// With Twig
$ twig = Twig:: create ( __DIR__ . ' /../templates ' , [ ' cache ' => false ]);
$ twig -> addExtension (FlashTwigExtension:: createFromContainer ( $ app ));
$ app -> add (TwigMiddleware:: create ( $ app , $ twig ));
}; // Your controller
//...
use Slim Views Twig ;
// use SlimFlashMessagesFlashProvider;
use SlimFlashMessages FlashProviderInterface ;
class YourController
{
private $ flash ;
private $ view ;
public function __construct ( FlashProviderInterface $ flash , Twig $ view )
{
$ this -> flash = $ flash ;
$ this -> view = $ view ;
}
public function index ( ServerRequestInterface $ request , ResponseInterface $ response )
{
// If you are working with middleware instead of dependency injection it will be this way.
// $flash = FlashProvider::fromRequest($request);
$ this -> flash -> add ( ' messages ' , ' Hello! ' );
return $ this -> view -> render ( $ response , ' template.twig ' );
}
//...
} {# template.twig #}
{% for msg in flash( ' messages ' ) %}
{{ msg }}
{% endfor %}在此示例中,您可以看到,树枝集成不是强制性的,其中重点是展示消息传递提供商API。
<?php
use Psr Http Message ResponseInterface as Response ;
use Psr Http Message ServerRequestInterface as Request ;
use Slim Factory AppFactory ;
use SlimFlashMessages Flash ;
use SlimFlashMessages FlashMiddleware ;
use SlimFlashMessages FlashProvider ;
require __DIR__ . ' /../vendor/autoload.php ' ;
// Important! if the storage is not passed to the constructor,
// $_SESSION will be used
$ flash = Flash:: getInstance ();
// Create App
$ app = AppFactory:: create ();
$ app -> setBasePath ( ' /example1 ' ); // Optional
// Add FlashMiddleware
$ app -> add ( new FlashMiddleware ( $ flash ));
$ app -> addErrorMiddleware ( true , true , true );
$ app -> get ( ' / ' , function ( Request $ request , Response $ response , $ args ) {
// Get FlashProvider from request
// FlashMiddleware previously took care of adding the FlashProvider to the request
$ flash = FlashProvider:: fromRequest ( $ request );
// Clear all stored values
$ flash -> clearAll ();
// The 'add' method allows you to add a flash message or data (as an array, if you prefer!)
$ flash -> add ( ' simple ' , ' Hello World! 1 ' );
$ flash -> add ( ' messages ' , [
' status ' => ' success ' ,
' text ' => ' 1. PHP is the best! '
]);
echo ' <pre> ' ;
var_dump ( $ flash -> getAll ());
// Checks if the key is defined in the storage
var_dump ( $ flash -> has ( ' messages ' ));
// Clear a key defined
$ flash -> clear ( ' messages ' );
var_dump ( $ flash -> getAll ());
var_dump ( $ flash -> has ( ' messages ' ));
$ flash -> add ( ' simple ' , ' Hello World! 2 ' );
$ flash -> add ( ' simple ' , ' Hello World! 3 ' );
var_dump ( $ flash -> getAll ());
// Get first item from key
var_dump ( $ flash -> get_first ( ' simple ' ));
// or to pick up and remove first item.
// var_dump($flash->get_first('simple', true));
// Get last item from key
// var_dump($flash->get_last('simple'));
// or to pick up and remove last item.
var_dump ( $ flash -> get_last ( ' simple ' , true ));
var_dump ( $ flash -> get ( ' simple ' ));
return $ response ;
});
$ app -> run ();此示例使用PHP-DI容器和Slim/Twig-view。
<?php
use DI Container ;
use Psr Http Message ResponseInterface as Response ;
use Psr Http Message ServerRequestInterface as Request ;
use Slim Factory AppFactory ;
use Slim Views Twig ;
use Slim Views TwigMiddleware ;
use SlimFlashMessages Flash ;
use SlimFlashMessages FlashMiddleware ;
use SlimFlashMessages FlashProvider ;
use SlimFlashMessages FlashProviderInterface ;
use SlimFlashMessages FlashTwigExtension ;
require __DIR__ . ' /../vendor/autoload.php ' ;
// Create a new DI Container
$ container = new Container ();
// Add a FlashProvider to the container
$ container -> set (FlashProviderInterface::class, function () {
// Important! if the storage is not passed to the constructor,
// $_SESSION will be used
return Flash:: getInstance ();
});
// Set container to create App with on AppFactory
AppFactory:: setContainer ( $ container );
$ app = AppFactory:: create ();
$ app -> setBasePath ( ' /example2 ' ); // Optional
// Add FlashMiddleware from container
$ app -> add (FlashMiddleware:: createFromContainer ( $ app ));
// Create Twig and add FlashTwigExtension
$ twig = Twig:: create ( __DIR__ . ' /templates ' , [ ' cache ' => false ]);
$ twig -> addExtension (FlashTwigExtension:: createFromContainer ( $ app ));
// Add Twig-View Middleware
$ app -> add (TwigMiddleware:: create ( $ app , $ twig ));
$ app -> addErrorMiddleware ( true , true , true );
$ app -> get ( ' / ' , function ( Request $ request , Response $ response , $ args ) {
// Get Twig and FlashProvider from request
$ view = Twig:: fromRequest ( $ request );
// FlashMiddleware previously took care of adding the FlashProvider to the request
$ flash = FlashProvider:: fromRequest ( $ request , ' flash ' );
$ alerts = [ ' primary ' , ' secondary ' , ' success ' , ' danger ' , ' warning ' , ' info ' , ' light ' , ' dark ' ];
// The 'add' method allows you to add a flash message or data (as an array, if you prefer!)
$ flash -> add ( ' simple ' , ' Hello World! ' );
$ flash -> add ( ' messages ' , [
' alert ' => $ alerts [ array_rand ( $ alerts )],
' text ' => ' 1. PHP is the best! '
]);
$ flash -> add ( ' messages ' , [
' alert ' => $ alerts [ array_rand ( $ alerts )],
' text ' => ' 2. Slim Framework is amazing! '
]);
$ flash -> add ( ' messages ' , [
' alert ' => $ alerts [ array_rand ( $ alerts )],
' text ' => ' 3. Lorem ipsum! '
]);
return $ view -> render ( $ response , ' template.html.twig ' , [
' page ' => ' Slim Flash Messages ' ,
]);
});
$ app -> run ();在模板中:
{% for msg in flash( ' messages ' ) %}
< div class = " alert alert-{{ msg . alert }} alert-dismissible fade show " role = " alert " >
{{ msg . text }}
< button type = " button " class = " btn-close " data-bs-dismiss = " alert " aria-label = " Close " ></ button >
</ div >
{% endfor %}运行这些示例的最简单方法是通过Docker。
git clone https://github.com/WilliamSampaio/Slim-Flash-Messages.git
cd Slim-Flash-Messages
docker compose up -d --build完成UP过程后,访问:
FlashTwigExtension将这些功能提供给您的树枝模板。
它接收两个可选参数, key (string/null = null )和clear (bool = true ) 。
key :如果未指定,则将返回带有所有存储数据的数组,否则只有带有密钥值索引的数据的数组将被返回。clear :如果是错误的,则调用功能后不会从存储中删除项目。 {% for msg in flash( ' messages ' , false ) %}
< div class = " alert alert-{{ msg . alert }} alert-dismissible fade show " role = " alert " >
{{ msg . text }}
< button type = " button " class = " btn-close " data-bs-dismiss = " alert " aria-label = " Close " ></ button >
</ div >
{% endfor %}它接收两个参数, key (字符串)和remove (bool = true ) 。
key :将返回带有键值索引的数据的第一个项目。remove (可选):如果是错误的,则调用功能后不会从存储中删除该项目。 {% set first = flash_first( ' messages ' ) %}
< div class = " alert alert-{{ first . alert }} alert-dismissible fade show " role = " alert " >
{{ first . text }}
< button type = " button " class = " btn-close " data-bs-dismiss = " alert " aria-label = " Close " ></ button >
</ div >它接收两个参数, key (字符串)和remove (bool = true ) 。
key :将返回带有键值索引的数据的最后一个项目。remove (可选):如果是错误的,则调用功能后不会从存储中删除该项目。 {% set last = flash_last( ' messages ' ) %}
< div class = " alert alert-{{ last . alert }} alert-dismissible fade show " role = " alert " >
{{ last . text }}
< button type = " button " class = " btn-close " data-bs-dismiss = " alert " aria-label = " Close " ></ button >
</ div >它接收一个参数, key (字符串) 。检查是否在存储中定义了键。返回true还是false 。
key :将要检查的密钥。 {{ flash_has( ' messages ' ) ? ' exists! ' : " it doesn't exist... " }}它接收一个可选参数, key (字符串) 。从存储中删除数据。返回void 。
key (可选):将要删除的键。如果未定义,它将从存储中删除所有数据。 {{ flash_clear( ' messages ' ) }}要执行测试套件,您需要克隆存储库并安装依赖项。
git clone https://github.com/WilliamSampaio/Slim-Flash-Messages.git
cd Slim-Flash-Messages
composer install
composer test
# Or
# composer coverage 麻省理工学院许可证(麻省理工学院)。请参阅许可证文件以获取更多信息。