m-adamski / symfony-helpers-bundle
此包已被放弃且不再维护。未建议替代包。
Symfony的助手包
2.0.3
2018-08-22 13:03 UTC
Requires
- php: ^7.1
- cakephp/chronos: ^1.1
- symfony/asset: ^4.0
- symfony/filesystem: ^4.0
- symfony/finder: ^4.0
- symfony/framework-bundle: ^4.0
- symfony/orm-pack: ^1.0
- symfony/swiftmailer-bundle: ^3.1
- symfony/translation: ^4.0
- symfony/twig-bundle: ^4.0
- tecnickcom/tcpdf: ^6.2
README
此包已弃用!每个助手都被移动到单独的包中
一套工具,用于改进常用功能的实现
- 面包屑助手
- 目录助手
- 邮件发送器助手
- 通知助手
- 分页助手
- PDF助手
面包屑助手
面包屑工具简化了面包屑的生成和显示过程。
如何使用它?
在控制器函数中,生成面包屑结构
$this->breadcrumbsHelper->addRouteItem("Management", "administrator.index", [], "navigation");
$this->breadcrumbsHelper->addRouteItem("Data management", "administrator.data", ["id" => $id], "navigation");
在Twig模板文件中使用breadcrumbs()
函数显示面包屑
<section class="breadcrumbs-container">
{{ breadcrumbs() }}
</section>
函数
- addItem(string $text, string $url = "", string $translationDomain = "breadcrumbs", array $translationParameters = [], bool $translate = true)
- addRouteItem(string $text, string $route, array $routeParameters = [], string $translationDomain = "breadcrumbs", array $translationParameters = [], bool $translate = true)
- addNamespaceItem(string $namespace, string $text, string $url = "", string $translationDomain = "breadcrumbs", array $translationParameters = [], bool $translate = true)
- prependItem(string $text, string $url = "", string $translationDomain = "breadcrumbs", array $translationParameters = [], bool $translate = true)
- prependRouteItem(string $text, string $route, array $routeParameters = [], string $translationDomain = "breadcrumbs", array $translationParameters = [], bool $translate = true)
- prependNamespaceItem(string $namespace, string $text, string $url = "", string $translationDomain = "breadcrumbs", array $translationParameters = [], bool $translate = true)
- getNamespaceBreadcrumbs(string $namespace = self::DEFAULT_NAMESPACE)
- clear(string $namespace = "")
目录助手
目录工具是为了更快地进行目录管理而创建的。借助它,您可以快速创建文件夹中所有文件的列表、创建新文件夹或生成文件路径。
邮件发送器助手
正如其名所示,邮件发送器助手用于发送电子邮件消息。助手包含两个函数
- buildMessage(?string $subject = null, ?string $body = null, ?string $contentType = null, ?string $charset = null)
- sendMessage(MailerMessage $mailerMessage, ?array &$failedRecipients = null)
第一个函数生成一个MailerMessage类的实例,该类扩展了Swift_Message的基本类。另一个尝试发送生成的消息。
如何使用它?
在控制器函数中,只需调用带有参数的sendMessage
函数
$messageSubject = "This is example message";
$messageTemplate = "mail/Notification/notification.html.twig";
$messageData = [
"template_parameter" => "Hello World!"
];
$messageAttachments = [
$this->directoryHelper->generatePath([
$this->directoryHelper->getPublicDirectory(), "download", "information.pdf"
], true)
];
// Send message
$this->mailerHelper->sendMessage(["john.sample@example.com"], $messageSubject, $messageTemplate, $messageData, $messageAttachments);
通知助手
此工具旨在帮助显示用户信息。
如何使用它?
例如:创建新的博客条目时发生错误,我们希望通知用户
$this->notificationHelper->addNotification(
NotificationHelper::ERROR_TYPE,
$this->translator->trans("An error occurred while trying to create a entry", [], "blog")
);
在Breadcrumbs Helper的情况下,我们还需要在模板中调用相应的函数
<section class="breadcrumbs-container">
{{ notification() }}
</section>
函数
- addNotification(string $type, string $text)
- redirectNotification(string $url, string $type, string $text)
- routeRedirectNotification(string $route, string $type, string $text, array $routeParams = [])
- getNotifications()
- clear()
分页助手
要使用分页助手,需要在控制器和实体仓库中进行一些更改。示例控制器函数
public function index(Request $request, int $page) {
return $this->render("modules/News/index.html.twig", [
"page" => $page,
"news" => $this->paginationHelper->responseData($request, $this->newsRepository, $page)
]);
}
如您所见,responseData
函数需要三个参数。其中之一是我们分页的实体的仓库。现在我们需要在实体仓库中做出更改 - 仓库类必须扩展 Adamski\Symfony\HelpersBundle\Model\PaginableRepository
并实现 getPaginated
函数
public function getPaginated(int $page = 1, int $limit = 20) {
$queryBuilder = $this->getEntityManager()->getRepository("App:News")
->createQueryBuilder("news");
$queryBuilder->where($queryBuilder->expr()->eq("news.public", true))
->orderBy("news.createdAt", "DESC");
return $this->paginate($queryBuilder->getQuery(), $page, $limit);
}
现在只需将函数添加到模板中渲染分页组件。
{{ pagination(news, "news.index", page) }}
PDF 助手
PDF 助手只提供了一个函数 - initDocument
。此函数创建 PDFDocument 对象,该对象提供了一些创建 PDF 文档时需要的有用函数。以下是使用 PDF 助手的示例
$pdfName = "Sample PDF document";
// Generate PDF template
$documentContent = $this->renderView("pdf/document-content.html.twig", [
"data" => $data
]);
// Generate PDF document
$pdfDocument = $this->pdfHelper->initDocument();
$pdfDocument->setTitle($pdfName);
$pdfDocument->setAuthor("Author");
$pdfDocument->setCreator("Creatot");
$pdfDocument->setFooter($pdfName);
$pdfDocument->writeHTML($documentContent);
// Generate response
$response = new Response(
$pdfDocument->output($pdfName)
);
$response->headers->set("Content-Type", "application/pdf");
$response->headers->set("Content-Disposition", "attachment; filename=\"" . $pdfName . ".pdf\"");
return $response;
在所示示例中,渲染了模板文件,然后 HTML 代码被插入为新的 PDF 文档的内容。此外,设置了 PDF 文件的参数。
许可
MIT