becklyn / route-tree-bundle
此包提供了一个由路由配置构建的树形结构的简单实现。
Requires
- php: >=7.2
- becklyn/menu: ^1.1.2 || ^2.0 || dev-legacy_support
- symfony/cache: ^4.3 || ^5.0
- symfony/http-foundation: ^4.3 || ^5.0
- symfony/http-kernel: ^4.3 || ^5.0
- symfony/routing: ^4.3 || ^5.0
- twig/twig: ^2.11 || ^3.0
Requires (Dev)
- roave/security-advisories: dev-master
- sensio/framework-extra-bundle: ^5.5
- symfony/phpunit-bridge: ^5.0
Suggests
- sensio/framework-extra-bundle: To automatically infer from @Security and @IsGranted annotations.
This package is auto-updated.
Last update: 2024-09-13 12:17:21 UTC
README
添加了一个用于自动生成路由树以构建菜单的简单实现。
安装
通过packagist安装此包
// ... require: { // ... "becklyn/route-tree-bundle": "^2.0" }, // ...
在您的app/AppKernel.php
中加载此包
public function registerBundles() { $bundles = array( // ... new \Becklyn\RouteTreeBundle\BecklynRouteTreeBundle(), ); // ... }
定义路由树节点
通过设置路由的选项向树中添加元素
homepage: path: / my_route: path: /my_route options: tree: parent: homepage title: "My Route"
路径选项
route: # ... options: tree: parent: homepage # the name of the parent node priority: 0 # (optional) the title: "abc" # (optional) title of the node parameters: {} # (optional) the default values for the parameters security: ~ # (optional) the security expression
parent
必须设置。所有引用的parent
-路由也需要存在。
优先级
所有节点的所有子节点都按降序优先级排序。
隐藏
如果项目没有设置标题(或安全表达式评估为false),所有项目将自动隐藏。
参数
参数可以定义参数的默认值
page_listing: path: /listing/{page} options: tree: parameters: page: 1
如果您没有定义值,则参数将在当前请求的请求属性中查找。如果那里找不到,则使用1
。
安全
每个节点都可以有一个自定义的安全表达式,当打印树时进行评估。所有评估为false
的安全表达式节点将自动隐藏(包括所有子节点)。
如果没有给出显式安全表达式,则构建器会尝试从链接控制器中推断表达式,通过组合动作方法和控制器类中的@IsGranted()
和@Security()
注解。如果使用带有subject
属性的@IsGranted()
,则不会进行推断。
额外参数
您可以定义额外的参数,这些参数可以在菜单模板中使用。所有未识别的路径选项(见“路径选项”)将自动添加为额外参数。
route: options: tree: parent: homepage title: Pages icon: pages
这些额外参数在模板中通过extra
属性访问
{{ item.extra("icon") }}
错误情况
如果页面树无效,则在第一次构建页面树时抛出InvalidRouteTreeException
异常。
简短语法
在简单情况下,配置可以简化为
route: options: tree: "parent" # ... means the same as ... route: options: tree: parent: "parent"
渲染路由树
有一个自动菜单构建器,您可以在模板中使用它
{{- route_tree_render("my_route", {...}) -}}
第一个参数是菜单应使用的父节点。第二个参数(可选)是KnpMenu渲染选项。
获取路由树
您可以将服务Becklyn\RouteTreeBundle\Tree\RouteTree
注入并使用它来检索节点
// get a node with a specific route. With this node you can traverse the route tree. $treeUnderMyRoute = $routeTree->getNode("my_route");
返回值是Becklyn\RouteTreeBundle\Node\Node
。