whitedigital-eu / site-tree
站点树
Requires
- php: >=8.2.0
- ext-dom: *
- api-platform/core: ^3
- doctrine/collections: *
- doctrine/dbal: *
- doctrine/orm: *
- gedmo/doctrine-extensions: ^3
- stof/doctrine-extensions-bundle: *
- symfony/config: ^6.3
- symfony/dependency-injection: ^6.3
- symfony/event-dispatcher: ^6.3
- symfony/framework-bundle: ^6.3
- symfony/http-foundation: ^6.3
- symfony/http-kernel: ^6.3
- symfony/property-access: ^6.3
- symfony/property-info: ^6.3
- symfony/routing: ^6.3
- symfony/serializer: ^6.3
- symfony/translation: ^6.3
- symfony/twig-bundle: ^6.3
- symfony/validator: ^6.3
- twig/twig: ^3
- whitedigital-eu/audit-service: ^0.6|^0.7
- whitedigital-eu/entity-resource-mapper-bundle: ^0.23|^0.24
Requires (Dev)
Conflicts
- dev-main / 0.3.x-dev
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-fix/improve-site-tree-resource-property-types
- dev-fix/fix-api-resource-id-types
- dev-feature/set-meta-tags
- dev-feature/remove-final-keyword
- dev-fix/audit-remove
- dev-feature/sitemap
- dev-feature/allow-passing-of-context-to-get-entity-class-method
- dev-feature/allow-overriding-of-api-resource-classes
- dev-develop
This package is auto-updated.
Last update: 2024-09-17 15:00:58 UTC
README
这是什么?
此软件包为api平台添加站点树功能,当与如vue等独立的客户端一起使用时,会增加后端对路由有效性的检查。
系统要求
PHP 8.2+
Symfony 6.2+
安装
推荐通过Composer进行安装
composer require whitedigital-eu/site-tree
配置
唯一必填的配置参数是 index_template
-> 当后端完成路由检查时作为响应包含的文件。
site_tree: index_template: index.html
use Symfony\Config\SiteTreeConfig; return static function (SiteTreeConfig $config): void { $config ->indexTemplate('index.html'); };
重要:给定给此参数的文件必须在为twig包配置的路径中
之后,您需要更新您的数据库模式以使用Audit实体。
如果使用迁移
bin/console doctrine:migrations:diff bin/console doctrine:migrations:migrate
如果通过模式更新
bin/console doctrine:schema:update --force
默认情况下,此软件包包含2个预定义的类型,用作站点树节点:Html
和 Redirect
。
Html
是一个简单的类型,没有任何额外逻辑,只有纯HTML内容。
Redirect
是一个类型,用于管理节点之间或到外部源的重定向。
要添加新类型,您需要
- 为此类型创建新的实体和资源
- 在配置中配置类型
site_tree: index_template: index.html types: news: ~ news2: entity: App\Entity\NotNews
use Symfony\Config\SiteTreeConfig; use App\Entity\NotNews; return static function (SiteTreeConfig $config): void { $config ->indexTemplate('index.html'); $config->types('news'); // or exact entity class if it is not default App\Entity\News $config->types('news2')->entity(NotNews::class); };
用法
要添加新的站点树节点,调用站点树API。默认:POST /api/site_trees
{ "title": "test", "slug": "test", "type": "html" }
这是创建新节点所需的最小数据。由于没有给出父节点,此调用将创建一个新的根节点。通常网站只需要一个节点,但如果存在多个菜单或其他需要新节点的链接,例如 登录
,则可以使用此软件包实现。
如果要向其他节点添加节点,只需添加 parent
参数
{ "title": "test", "slug": "test", "type": "html", "parent": "/api/site_trees/1" }
要查询所有节点,调用 GET /api/site_trees
或要查询带有子节点的根节点,调用 GET /api/site_trees?level=0
此外 此软件包提供检查URL是否定义在任何节点中的功能。要检查URL是否有效,调用 GET /api/content_types/<url>
。
例如,从上面的示例中,GET /api/content_types/test
将返回:200 OK
+
{ "nodeId": 1, "node": "/api/site_trees/1", "type": "resource" }
但 GET /api/content_types/test2
将返回:404 Not Found
+
{ "type": "https://tools.ietf.org/html/rfc2616#section-10", "title": "An error occurred", "detail": "Not Found" }
其他配置参数
由于此库使用路由监听器在调用任何URL以包含索引模板文件时覆盖路由器,您可能需要过滤出不应执行此操作的路径。
要配置这些路径,您可以这样做
site_tree: index_template: index.html excluded_path_prefixes: - '/admin' excluded_path_prefixes_dev: - '/test'
use Symfony\Config\SiteTreeConfig; return static function (SiteTreeConfig $config): void { $config ->indexTemplate('index.html') ->excludedPathPrefixes([ '/admin', ]) ->excludedPathPrefixesDev([ '/test', ]); };
此配置跳过了这些定义路径开始的任何路径的监听器逻辑。
默认情况下,此包已经跳过了一些路径
在任何环境中
/api
/sitemap.xml
在dev/test环境中
/_profiler
/_wdt
/_error
为了更高级的安全,所有自定义内容类型都应该扩展 AbstractContentTypeProvider
如果您有任何公开访问的自定义内容类型,您应使用 AbstractContentTypeProvider
作为基础提供程序,以便在公开访问时只能访问来自活动站点树节点的类型数据。
use WhiteDigital\SiteTree\DataProvider\AbstractContentTypeProvider; class CustomContentTypeDataProvider extends AbstractContentTypeProvider { // ... }
如果此扩展不可行,您可以使用 LimitContentTypePublicAccessTrait
来获取用于与 Doctrine\Orm\QueryBuilder
一起使用的限制功能,用于集合和单个项。
覆盖软件包的部分
覆盖默认API资源(以及因此API端点)
默认情况下,SiteTree软件包资源基于 src/Api/Resource
目录内容。
如果您不想使用这些资源以及不公开它们提供的API端点,只需使用配置值设置自定义API资源路径。如果您将其设置为null
,API平台将不会注册此包中定位的API资源。
site_tree: custom_api_resource_path: '%kernel.project_dir%/src/MyCustomPath' # custom_api_resource_path: null
use Symfony\Config\SiteTreeConfig; return static function (SiteTreeConfig $config): void { $config ->customApiResourcePath('%kernel.project_dir%/src/MyCustomPath') // or ->customApiResourcePath(null); };
在覆盖默认API资源后,不要忘记更新用于在whitedigital-eu/entity-resource-mapper-bundle
中执行资源与实体映射的ClassMapperConfigurator配置。
use App\ApiResource\Admin\SiteTreeResource; use WhiteDigital\SiteTree\Entity\SiteTree; use WhiteDigital\EntityResourceMapper\Mapper\ClassMapper; use WhiteDigital\EntityResourceMapper\Mapper\ClassMapperConfiguratorInterface; final class ClassMapperConfigurator implements ClassMapperConfiguratorInterface { public function __invoke(ClassMapper $classMapper): void { $classMapper->registerMapping(SiteTreeResource::class, SiteTree::class); } }
网站地图
要将网站地图添加到GET /sitemap.xml
,您需要向项目路由中添加路由配置。
# config/routes/site-tree.yaml site_tree: resource: '../vendor/whitedigital-eu/site-tree/src/Controller/' type: attribute
// config/routes/site-tree.php use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return static function (RoutingConfigurator $routes): void { $routes->import('../vendor/whitedigital-eu/site-tree/src/Controller/', 'attribute'); };
网站地图仅返回启用路由,条件是isActive: true
和isVisible: true
。如果您想包括不可见(但仍然活跃)的路由,这些路由具有isActive: true
和isVisible: false
,请在配置中进行设置。
site_tree: #... sitemap: include_invisible: true
use Symfony\Config\SiteTreeConfig; return static function (SiteTreeConfig $config): void { $config ->sitemap() ->includeInvisible(true); };