Dump your Symfony app to HTML + CSS + JS only static website. Useful for deploy to Github Pages and other non-PHP static website hostings.
composer require symplify/symfony-static-dumper
Add config to config/config.php
:
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyStaticDumper\ValueObject\SymfonyStaticDumperConfig;
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(SymfonyStaticDumperConfig::FILE_PATH);
};
To make Controller with argument, eg: /blog/{slug}
, statically dumped, you have to implements Symplify\SymfonyStaticDumper\Contract\ControllerWithDataProviderInterface
and implements 3 methods:
getControllerClass()
getControllerMethod()
getArguments()
For example, with the following provider:
namespace TomasVotruba\SymfonyStaticDump\ControllerWithDataProvider;
use Symplify\SymfonyStaticDumper\Contract\ControllerWithDataProviderInterface;
use TomasVotruba\Blog\Controller\PostController;
use TomasVotruba\Blog\Repository\PostRepository;
final class PostControllerWithDataProvider implements ControllerWithDataProviderInterface
{
private PostRepository $postRepository;
public function __construct(PostRepository $postRepository)
{
$this->postRepository = $postRepository;
}
public function getControllerClass(): string
{
return PostController::class;
}
public function getControllerMethod(): string
{
return '__invoke';
}
/**
* @return string[]
*/
public function getArguments(): array
{
$slugs = [];
foreach ($this->postRepository->getPosts() as $post) {
$slugs[] = $post->getSlug();
}
return $slugs;
}
}
For the following controller:
namespace TomasVotruba\Blog\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use TomasVotruba\Blog\Repository\PostRepository;
use TomasVotruba\Blog\ValueObject\Post;
final class PostController extends AbstractController
{
private PostRepository $postRepository;
public function __construct(PostRepository $postRepository)
{
$this->postRepository = $postRepository;
}
/**
* @Route(path="/blog/{slug}", name="post_detail", requirements={"slug"="\d+\/\d+.+"})
*/
public function __invoke(string $slug): Response
{
$post = $this->postRepository->getBySlug($slug);
return $this->render('blog/post.twig', [
'post' => $post,
'title' => $post->getTitle(),
]);
}
}
vendor/bin/console dump-static-site
The website will be generated to /output
directory in your root project.
Do you want to modify the /public
directory yourself?
vendor/bin/console dump-static-site --public-directory another-public --output-directory custom-output
To see the website, just run local server:
php -S localhost:8001 -t output
And open localhost:8001 in your browser.
In case you are experiencing a bug or want to request a new feature head over to the Symplify monorepo issue tracker
The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.