vivait / bootstrap-bundle
Bootstrap通用包
Requires
- knplabs/knp-menu-bundle: ~2.1
- moment/moment: ~2.5
- mopa/bootstrap-bundle: ~3.0
- twbs/bootstrap: v3.2.0
- vivait/common: ~2.2
This package is auto-updated.
Last update: 2021-06-14 12:56:15 UTC
README
一组常用模板和工具,用于协助在Symfony中快速开发应用程序。
安装
使用Composer
$ composer require vivait/bootstrap-bundle
启用包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Vivait\BootstrapBundle\VivaitBootstrapBundle() ); }
添加配置规则
将以下内容添加到您的config.yml中,以启用Mopa Bootstrap集成
mopa_bootstrap: form: show_legend: false
将以下内容添加到您的config.yml中,以在Assetic中启用Mopa Bootstrap集成:注意:Bootstrap 3.1及以上版本使用less的附加功能,这些功能与LESS编译器不兼容,因此我们已将其删除,直到找到解决方案为止
assetic: bundles: [VivaitBootstrapBundle,MopaBootstrapBundle] filters: cssrewrite: ~
如果您已经在config.yml中有了assetic配置,则应合并这两个配置,例如。
assetic: debug: %assetic_debug% use_controller: false bundles: [VivaitBootstrapBundle,MopaBootstrapBundle] filters: cssrewrite: ~
将以下内容添加到您的config.yml中,以启用Viva Bootstrap表单集成
twig: form: resources: - 'VivaitBootstrapBundle:Form:fields.html.twig' globals: viva_app_name: My App name
如果您已经在config.yml中有了twig配置,则应合并这两个配置,例如。
twig: debug: %kernel.debug% strict_variables: %kernel.debug% form: resources: - 'VivaitBootstrapBundle:Form:fields.html.twig' globals: viva_app_name: My App name
您可以将viva_app_name
更改为应用程序的标题,这将随后出现在每个页面的标题中。
导出Assetic文件
php app/console mopa:bootstrap:symlink:less php app/console mopa:bootstrap:install:font php app/console assets:install --symlink php app/console assetic:dump php app/console cache:clear
更新composer.json
要使Mopa bootstrap bundle在安装时自动执行符号链接,请将以下内容添加到您的composer.json中
"scripts": { "post-install-cmd": [ "Mopa\\Bundle\\BootstrapBundle\\Composer\\ScriptHandler::postInstallSymlinkTwitterBootstrap" ], "post-update-cmd": [ "Mopa\\Bundle\\BootstrapBundle\\Composer\\ScriptHandler::postInstallSymlinkTwitterBootstrap" ] },
启用搜索框
要启用搜索框,您需要在您的config.yml
中定义搜索控制器的路由,例如
twig: globals: vivait_search_path: myapp_customers_search
这然后将搜索查询通过query
GET参数传递给您的控制器。
使用KNP菜单
使用Composer
$ composer require knplabs/knp-menu-bundle
启用包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Knp\Bundle\MenuBundle\KnpMenuBundle() ); }
添加菜单项
要添加菜单项,您需要创建一个事件监听器,该监听器将监听vivait.bootstrap.menu_configure
事件
<?php // src/MyApp/MyBundle/EventListener.php namespace MyApp\MyBundle\EventListener; use Vivait\BootstrapBundle\Event\ConfigureMenuEvent; class ConfigureMenuListener { /** * @param ConfigureMenuEvent $event */ public function onMenuConfigure(ConfigureMenuEvent $event) { $menu = $event->getMenu() ->getChild('main'); $members = $menu->addChild('Customers', array( 'dropdown' => true, 'caret' => true, )); $members->addChild('Dashboard', array( 'icon' => 'home', 'route' => 'myapp_customers_list' )); $members->addChild('Add new', array( 'icon' => 'plus', 'route' => 'myapp_customers_add' )); // ... etc. } } ?>
然后您需要配置您的services.yml
中的此事件
myapp.mybundle.configure_menu_listener: class: MyApp\MyBundle\EventListener\ConfigureMenuListener tags: - { name: kernel.event_listener, event: vivait.bootstrap.menu_configure, priority: -2, method: onMenuConfigure }
使用列表水化器
Bootstrap bundle包含一个基于此博客文章的定制Doctrine水化器。
这个水合器非常简单,它会检查结果集每行返回的列。如果只有两列,则假定第一列是键字段(通常是对象的ID),第二列是值字段。如果每行有多于两列,则返回的数组将是一个ID索引数组,每行由剩余列值的数组组成。
要启用水合器的使用,请在您的config.yml中添加以下内容:
orm: hydrators: ListHydrator: \Vivait\BootstrapBundle\Hydrator\ListHydrator
对于与Doctrine ORM 2.5+一起使用,请使用\Vivait\BootstrapBundle\Hydrator\ListHydrator25
并在查询结果检索时使用它。
$results = $this->getDoctrine()->getManager()->createQuery('{query}')->getResult('ListHydrator');
使用用户可调用函数
在您的应用程序中的某个时刻,您可能希望通过容器注入当前用户。Bootstrap提供了一个辅助类来完成这个任务,基于这个StackOverflow回答。
只需注入vivait.bootstrap.user.callable
,如下所示:
class: \My\Class arguments: [@vivait.bootstrap.user.callable]
然后,当您需要在类中引用当前用户时,只需调用userCallable::getCurrentUser
,如下所示:
private $userCallable; function __construct(UserCallable $userCallable) { $this->userCallable = $userCallable; } public function mailCurrentUser() { mail($userCallable->getCurrentUser()->getEmail(), 'Example', 'Please don\'t actually use this example method!'); }