ไลบรารีนี้ช่วยให้คุณสามารถใช้ข้อความชั่วคราวในโครงการ Slim ของคุณ มันถูกรวมเข้ากับระบบเทมเพลต Twig ได้อย่างง่ายดายผ่านส่วนขยายที่ให้ฟังก์ชั่นในการคว้าและใช้ในเทมเพลต ไม่ จำกัด เพียงการสร้างสตริงข้อความอย่างง่าย แต่ยังอนุญาตให้ใช้ประเภทข้อมูลอื่น ๆ เช่นอาร์เรย์
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 %}การรวม Twig นั้นไม่ได้บังคับอย่างที่คุณเห็นในตัวอย่างนี้โดยมุ่งเน้นที่จะแสดงให้เห็นถึง 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 ให้ฟังก์ชั่นเหล่านี้กับเทมเพลต Twig ของคุณ
มันได้รับพารามิเตอร์เสริมสอง key (สตริง/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 ใบอนุญาต MIT (MIT) โปรดดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม