探索/状态

操作状态公告

2.0 2023-12-02 22:27 UTC

This package is auto-updated.

Last update: 2024-10-01 00:06:48 UTC


README

此包旨在通过消除对会话闪存袋的繁琐检查来帮助设置控制器的操作状态。

安装(通过composer)

要使用Composer安装StatusBundle,只需将以下内容添加到您的composer.json文件中

{
    // ...
    require: {
        "exploring/status": "dev-master"
    }
}

请将dev-master替换为某个具体的发布标签,例如,1.*

然后运行

php composer.phar update

并在AppKernel.php中注册该包

$bundles = array(
    ....
    new \Exploring\StatusBundle\ExploringStatusBundle(),
);

您就可以开始使用了。

配置

此包的配置简单。为了使其工作,您只需要

exploring_status: ~

操作状态可以存储在session(默认)或apc中。为了使用apc模式,根据您的PHP版本,您可能需要先安装它。

这可以通过指定engine配置条目来完成

exploring_status:
    engine: apc

设置操作状态

$manager = $this->get('exploring_status.manager');

$manager->success('Yey, you did it!');

$manager->warning('Ok, everything went ok, but there is something fishy going on here.');

$manager->error('Couldn\'t do it :(');

检索操作状态

您可以在控制器内部或直接从Twig中执行此操作。

PHP

$manager = $this->get('exploring_status.manager');

// Get first status, if exists
// This returns object of `StatusObject` type
$status = $manager->first();

// Get all status messages
// This returns `array` of `StatusObject` objects
$all = $manager->all();

Twig

{# This prints first status message directly #}
{{ ExploringStatus_First() }}

{# This prints all status messages directly #}
{{ ExploringStatus_All() }}

Twig模板可以轻松覆盖。有关如何操作的说明,请参阅官方文档

消息组

状态消息可以被分组。事实上,当您设置状态消息时,默认会进入默认组。

$manager = $this->get('exploring_status.manager');

// This message will go into `happiness` group
$manager->success('Yey, you did it!', 'happiness');

// This one goes into `Default`
$manager->warning('Ok, everything went ok, but there is something fishy going on here.');

// This one goes into `Fatal`
$manager->error('Couldn\'t do it :(', 'Fatal');

对于检索,规则与上面定义的相同 - 您只需要传递组名作为参数。

PHP

// Get first status from group `happiness`
// This returns object of `StatusObject` type
$status = $manager->first('happiness');

// Get all status messages from group `Fatal`
// This returns `array` of `StatusObject` objects
$all = $manager->all('Fatal');

Twig

{# This prints first status message directly #}
{{ ExploringStatus_First('happiness') }}

{# This prints all status messages directly #}
{{ ExploringStatus_All('Fatal') }}