norberttech/static-content-generator-bundle

从您的symfony应用程序生成静态HTML页面

0.6.1 2024-02-10 20:24 UTC

This package is auto-updated.

Last update: 2024-09-10 21:47:27 UTC


README

Tests Minimum PHP Version Minimum Symfony Version

从系统中的所有Symfony路由生成静态HTML页面。

安装

composer require norberttech/static-content-generator-bundle

配置

<?php
// bundles.php

return [
    NorbertTech\StaticContentGeneratorBundle\StaticContentGeneratorBundle::class => ['all' => true],
];
# static_content_generator.yaml

static_content_generator:
  output_directory: "%kernel.project_dir%/output"

使用

将所有路由转换为静态HTML页面。

bin/console static-content-generator:generate:routes

选项

  • --parallel=4 - 同时使用4个子进程并行生成静态内容
  • --clean - 在开始前清理输出路径
  • --filter-route - 仅生成指定路由的内容
  • --filter-route-prefix - 生成具有指定前缀的路由的内容
  • --exclude-route - 生成除了指定路由之外的所有路由的内容
  • --exclude-route-prefix - 生成除了具有指定前缀的路由之外的所有路由的内容

将所有资源从public目录复制到输出目录

bin/console static-content-generator:copy:assets

参数化路由

为了导出参数化路由,您需要注册实现SourceProvider接口的服务,该服务将返回您想要导出的给定路由的所有可能的参数组合。

别忘了这个服务必须有static_content_generator.source_provider标签。

# service.yaml 

services:

    FixtureProject\Source\ParametrizedSourceProvider:
        tags: ['static_content_generator.source_provider']

控制器

<?php declare(strict_types=1);

namespace FixtureProject\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class StaticRoutesController extends AbstractController
{
    /**
     * @Route("/parametrized/{param1}/{param2}", name="parametrized_route")
     */
    public function withParameters(string $param1, string $param2) : Response
    {
        return $this->render('parametrized.html.twig', ['param1' => $param1, 'param2' => $param2]);
    }
}

提供者

<?php

declare(strict_types=1);

namespace FixtureProject\Source;

use NorbertTech\StaticContentGeneratorBundle\Content\Source;
use NorbertTech\StaticContentGeneratorBundle\Content\SourceProvider;

final class ParametrizedSourceProvider implements SourceProvider
{
    public function all() : array
    {
        return [
            new Source('parametrized_route', ['param1' => 'first-param', 'param2' => 'second-param']),
        ];
    }
}

这将生成/output/parametrized/first-param/second-param/index.html

测试

php -S localhost:8000 -t output