{"id":13805845,"url":"https://github.com/slimphp/Slim-Flash","last_synced_at":"2025-05-13T21:31:36.874Z","repository":{"id":29722607,"uuid":"33265769","full_name":"slimphp/Slim-Flash","owner":"slimphp","description":"Slim Framework flash messages service provider","archived":false,"fork":false,"pushed_at":"2022-02-10T09:41:16.000Z","size":626,"stargazers_count":149,"open_issues_count":3,"forks_count":36,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-05-18T04:41:12.453Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"dsldevkit/dsl-devkit","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slimphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-01T18:43:46.000Z","updated_at":"2024-04-16T21:17:40.000Z","dependencies_parsed_at":"2022-08-27T16:20:46.976Z","dependency_job_id":null,"html_url":"https://github.com/slimphp/Slim-Flash","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FSlim-Flash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FSlim-Flash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FSlim-Flash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FSlim-Flash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slimphp","download_url":"https://codeload.github.com/slimphp/Slim-Flash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254031058,"owners_count":22002692,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T01:01:05.523Z","updated_at":"2025-05-13T21:31:36.573Z","avatar_url":"https://github.com/slimphp.png","language":"PHP","funding_links":[],"categories":["Miscellaneous","Packages and Middleware"],"sub_categories":["Videos"],"readme":"# Slim Framework Flash Messages\n\n[![Build Status](https://travis-ci.org/slimphp/Slim-Flash.svg?branch=master)](https://travis-ci.org/slimphp/Slim-Flash)\n\nThis repository contains a Slim Framework Flash messages service provider. This enables you to define transient messages that persist only from the current request to the next request.\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require slim/flash\n```\n\nRequires Slim 3.0.0 or newer.\n\n## Usage\n\n### Slim 4\n\nThis example assumes that you have `php-di/php-di` installed.\n\n```php\n\u003c?php\n\nuse DI\\ContainerBuilder;\nuse Slim\\Factory\\AppFactory;\nuse Slim\\Flash\\Messages;\nuse Slim\\Routing\\RouteContext;\n\nrequire_once __DIR__ . '/../vendor/autoload.php';\n\n$containerBuilder = new ContainerBuilder();\n\n// Add container definition for the flash component\n$containerBuilder-\u003eaddDefinitions(\n    [\n        'flash' =\u003e function () {\n            $storage = [];\n            return new Messages($storage);\n        }\n    ]\n);\n\nAppFactory::setContainer($containerBuilder-\u003ebuild());\n\n$app = AppFactory::create();\n\n// Add session start middleware\n$app-\u003eadd(\n    function ($request, $next) {\n        // Start PHP session\n        if (session_status() !== PHP_SESSION_ACTIVE) {\n            session_start();\n        }\n\n        // Change flash message storage\n        $this-\u003eget('flash')-\u003e__construct($_SESSION);\n\n        return $next-\u003ehandle($request);\n    }\n);\n\n$app-\u003eaddErrorMiddleware(true, true, true);\n\n// Add routes\n$app-\u003eget(\n    '/',\n    function ($request, $response) {\n        // Set flash message for next request\n        $this-\u003eget('flash')-\u003eaddMessage('Test', 'This is a message');\n\n        // Redirect\n        $url = RouteContext::fromRequest($request)-\u003egetRouteParser()-\u003eurlFor('bar');\n\n        return $response-\u003ewithStatus(302)-\u003ewithHeader('Location', $url);\n    }\n);\n\n$app-\u003eget(\n    '/bar',\n    function ($request, $response) {\n        $flash = $this-\u003eget('flash');\n\n        // Get flash messages from previous request\n        $messages = $flash-\u003egetMessages();\n        print_r($messages);\n\n        // Get the first message from a specific key\n        $test = $flash-\u003egetFirstMessage('Test');\n        print_r($test);\n\n        return $response;\n    }\n)-\u003esetName('bar');\n\n$app-\u003erun();\n```\n\n### Slim 3\n\n```php\n// Start PHP session\nsession_start();\n\n$app = new \\Slim\\App();\n\n// Fetch DI Container\n$container = $app-\u003egetContainer();\n\n// Register provider\n$container['flash'] = function () {\n    return new \\Slim\\Flash\\Messages();\n};\n\n$app-\u003eget('/foo', function ($req, $res, $args) {\n    // Set flash message for next request\n    $this-\u003eflash-\u003eaddMessage('Test', 'This is a message');\n\n    // Redirect\n    return $res-\u003ewithStatus(302)-\u003ewithHeader('Location', '/bar');\n});\n\n$app-\u003eget('/bar', function ($req, $res, $args) {\n    // Get flash messages from previous request\n    $messages = $this-\u003eflash-\u003egetMessages();\n    print_r($messages);\n\n    // Get the first message from a specific key\n    $test = $this-\u003eflash-\u003egetFirstMessage('Test');\n    print_r($test);\n});\n\n$app-\u003erun();\n```\n\n\u003e Please note that a message could be a string, object or array. Please check what your storage can handle.\n\n### Using with Twig-View\n\nIf you use [Twig-View](https://github.com/slimphp/Twig-View), then [slim-twig-flash](https://github.com/kanellov/slim-twig-flash) may be a useful integration package.\n\n\n## Testing\n\n``` bash\n$ phpunit\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security\n\nIf you discover any security related issues, please email security@slimframework.com instead of using the issue tracker.\n\n## Credits\n\n- [Josh Lockhart](https://github.com/codeguy)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslimphp%2FSlim-Flash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslimphp%2FSlim-Flash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslimphp%2FSlim-Flash/lists"}