becklyn / page-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: 2023-01-17 12:29:58 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
。