zenstruck/controller-util

此包已被废弃,不再维护。未建议替换包。

用于创建无依赖的 Symfony2 响应的实用工具。

v0.6.0 2015-12-15 14:27 UTC

README

Build Status Scrutinizer Code Quality Code Coverage SensioLabs Insight StyleCI Latest Stable Version License

在创建作为服务器的 Symfony2 控制器时,您通常需要为每个控制器相同的依赖项。

您通常需要

  • 用于生成重定向的路由器。
  • 用于添加闪存消息的会话。
  • 用于创建视图的模板引擎。
  • 用于转发到另一个控制器的内核。
  • 如果使用 jms/serializer,则序列化器。

此库旨在通过使您的控制器能够返回用于这些任务的小型不可变对象来删除这些常见依赖项。视图监听器随后获取这些对象并创建响应。

有一个 Symfony2 Bundle 和一个 Silex Service Provider 可用于简化项目集成。

用法

转发

要将请求转发到另一个控制器,请返回 Zenstruck\ControllerUtil\Forward 对象。

use Zenstruck\ControllerUtil\Forward;

// ...
public function forwardAction()
{
    return new Forward('another.controller:anotherAction', array('foo' => 'bar'));
}
// ...

参数

  • $controller: 转发到的控制器(必需)。
  • $parameters: 要传递给控制器的参数数组(默认:array())。

重定向

要将重定向到另一个路由,请返回 Zenstruck\ControllerUtil\Redirect 对象。

use Zenstruck\ControllerUtil\Redirect;

// ...
public function redirectAction()
{
    return new Redirect('my_route');

    // with parameters
    return new Redirect('my_route', array('foo' => 'bar'));
}
// ...

参数

  • $route: 要重定向到的路由(必需)。
  • $parameters: 路由所需的参数数组(默认:array())。
  • $statusCode: 响应的状态码(默认:302)。

闪存重定向

要将重定向到另一个路由并添加闪存消息,请返回 Zenstruck\ControllerUtil\FlashRedirect 对象。

use Zenstruck\ControllerUtil\FlashRedirect;

// ...
public function redirectAction()
{
    return new FlashRedirect('my_route', array('foo' => 'bar'), array('info' => array('Success!'));

    // factory methods
    return FlashRedirect::create('my_route', array('foo' => 'bar'), 'Error', 'error');
    return FlashRedirect::createSimple('my_route', 'Success');
}
// ...

参数

  • $route: 要重定向到的路由(必需)。
  • $parameters: 路由所需的参数数组(默认:array())。
  • $flashes: 闪存消息数组(默认:array())。
  • $statusCode: 响应的状态码(默认:302)。

注意:闪存必须以下格式为数组:array($key => array($message))。由于这可能很麻烦,因此存在工厂方法。

工厂方法

  • FlashRedirect::create:

    参数

    • $route: 要重定向到的路由(必需)。
    • $parameters: 路由所需的参数数组(必需)。
    • $message: 闪存消息(必需)。
    • $type: 闪存类型(默认:info)。
    • $statusCode: 响应的状态码(默认:302)。
  • FlashRedirect::createSimple(用于无路由参数的重定向)

    参数

    • $route: 要重定向到的路由(必需)。
    • $message: 闪存消息(必需)。
    • $type: 闪存类型(默认:info)。
    • $statusCode: 响应的状态码(默认:302)。

视图

要为您的响应创建视图,请返回 Zenstruck\ControllerUtil\View 对象。此库有 3 个视图监听器

  • TemplatingViewListener:用于使用 Symfony2 模板组件渲染视图。
  • TwigViewListener:用于使用 Twig 渲染视图。
  • SerializerViewListener:用于使用 jms/serializer 渲染非 HTML 视图。
use Zenstruck\ControllerUtil\View;

// ...
public function viewAction()
{
    $object = // ..

    return new View($object, 200);

    // with templates
    return new View($object, 200, 'my_template.html.twig');

    // with an array of fallback templates
    return new View($object, 200, array('my_template.html.twig', 'fallback_template.html.twig'));

    // factory methods
    return View::createCached($object, 86400);
}
// ...

参数

  • $data:传递给视图的数据。
  • $statusCode:响应的状态码(默认:200)。
  • $template:模板或模板数组(默认:null)。
  • $cache:响应的缓存选项数组(默认:array())。
  • $headers:响应头数组(默认:array())。

工厂方法

  • View::createCached:

    参数

    • $data:传递给视图的数据(必需)。
    • $sharedMaxAge:共享最大存活时间(秒)(必需)。
    • $statusCode:响应的状态码(默认:200)。

注意事项:

  • $template是模板数组时,视图监听器将遍历它们,并渲染存在的第一个。
  • 如果没有提供模板,您需要启用SerializerViewListener,并且请求必须是非HTML。否则将引发错误。
  • 如果$data不是数组,提供了模板,视图监听器将在将其传递给模板之前将$data转换为array('data' => $data)

无内容视图

该库有一个NoContentViewListener,允许您的控制器返回一个空的视图或(如果启用)简单地返回null。视图监听器将设置无内容响应(204)。

use Zenstruck\ControllerUtil\View;

// ...
public function viewAction()
{
    return new View(null);

    return null; // if enabled
}
// ...

模板

如果您的视图始终具有模板,您可以使用Zenstruck\ControllerUtil\Template对象以提高方便性。

use Zenstruck\ControllerUtil\Template;

// ...
public function viewAction()
{
    $object = // ..

    return new Template('my_template.html.twig', array('object' => $object));
}
// ...

参数

  • $template:模板或模板数组(必需)。
  • $parameters:传递给视图的参数(默认:array())。
  • $statusCode:响应的状态码(默认:200)。
  • $cache:响应的缓存选项数组(默认:array())。
  • $headers:响应头数组(默认:array())。

工厂方法

  • Template::createCached:

    参数

    • $template:模板或模板数组(必需)。
    • $sharedMaxAge:共享最大存活时间(秒)(必需)。
    • $parameters:传递给视图的参数(默认:array())。
    • $statusCode:响应的状态码(默认:200)。

手动安装

建议您使用Symfony2 BundleSilex Service Provider来将实用工具包含到您的项目中。

如果您正在使用Symfony2事件分发器进行自定义操作,您可以手动注册监听器

// add the HasFlashesListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new HasFlashesListener($flashBag), 'onKernelView'),
    10 // before other events
);

// add the RedirectListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new RedirectListener($urlGenerator), 'onKernelView')
);

// add the ForwardListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new ForwardListener(), 'onKernelView')
);

// add the TwigViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new TwigViewListener($twigEnvironment), 'onKernelView')
);

// add the TemplatingViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new TemplatingViewListener($templating), 'onKernelView')
);

// add the NoContentViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new NoContentViewListener(true /* false to force an empty view and not allow null */), 'onKernelView'),
    7 // before other events
);

// add the SerializerViewListener
$eventDispatcher->addListener(
    KernelEvents::VIEW,
    array(new SerializerViewListener($serializer), 'onKernelView'),
    5 // before other events
);

注意事项:

  • 注意HasFlashesListenerNoContentViewListenerSerializerViewListener的优先级。这些监听器需要在其他监听器之前触发。
  • 您应该只使用TemplatingViewListenerTwigViewListener中的一个,而不是两者都使用。