https://github.com/doctrine/symfony-doctrine-static-website-generator
https://github.com/doctrine/symfony-doctrine-static-website-generator
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/doctrine/symfony-doctrine-static-website-generator
- Owner: doctrine
- Created: 2018-10-18T04:27:11.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-22T23:27:41.000Z (almost 8 years ago)
- Last Synced: 2025-04-05T15:51:25.458Z (over 1 year ago)
- Language: PHP
- Size: 31.3 KB
- Stars: 14
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Symfony & Doctrine Static Website Generator
This is an example Symfony Flex application which pulls together the following
packages to create a static website generator:
- [doctrine/skeleton-mapper](https://github.com/doctrine/skeleton-mapper)
- [doctrine/static-website-generator](https://github.com/doctrine/static-website-generator)
- [doctrine/doctrine-skeleton-mapper-bundle](https://github.com/doctrine/DoctrineSkeletonMapperBundle)
- [doctrine/doctrine-static-website-generator-bundle](https://github.com/doctrine/DoctrineStaticWebsiteGeneratorBundle)
## Setup
First clone the repository:
$ git clone git@github.com:jwage/symfony-doctrine-static-website-generator.git
Now run composer install:
$ cd symfony-doctrine-static-website-generator
$ composer install
## Building
Now you are ready to build the example static website:
$ php bin/console doctrine:build-website
## Usage
### Routes
Take a look at the parameter named `doctrine.static_website.routes` in [config/routes.yml](https://github.com/jwage/symfony-doctrine-static-website-generator/blob/master/config/services.yaml)
for defining routes for your static website.
```yaml
parameters:
doctrine.static_website.routes:
homepage:
path: /index.html
controller: App\Controller\UserController::index
user:
path: /user/{username}.html
controller: App\Controller\UserController::user
provider: App\Requests\UserRequests::getUsers
```
### Controllers
In your `src/Controller` directory, you can create plain old PHP classes for controllers and map them
using routes.
Once you have created a controller, you have to configure a `ControllerProvider` in the
`config/services.yaml` file and pass it your controllers:
```yaml
services:
# ...
Doctrine\StaticWebsiteGenerator\Controller\ControllerProvider:
arguments:
-
- '@App\Controller\UserController'
```
Here is what the `UserController` looks like:
```php
userRepository = $userRepository;
}
public function index() : Response
{
$users = $this->userRepository->findAll();
return new Response([
'users' => $users
]);
}
public function user(string $username) : Response
{
$user = $this->userRepository->findOneByUsername($username);
return new Response([
'user' => $user,
], '/user.html.twig');
}
}
```
### Data Sources
Data sources are a simple way to use a PHP service to provide data for a Doctrine Skeleton Mapper model. They
are contained in the `src/DataSources` directory and must implement the `Doctrine\SkeletonMapper\DataSource\DataSource`
interface.
Here is what the `Users` data source looks like:
```php
'jwage'],
['username' => 'ocramius'],
['username' => 'ccovey'],
];
}
}
```
### Models
Models are contained in the `src/Models` directory and are plain PHP classes
that must implement the following interfaces:
- `Doctrine\SkeletonMapper\Hydrator\HydratableInterface`
- `Doctrine\SkeletonMapper\Mapping\LoadMetadataInterface`
Here is what the `User` model looks like:
```php
setIdentifier(['username']);
}
/**
* @param mixed[] $user
*/
public function hydrate(array $user, ObjectManagerInterface $objectManager) : void
{
$this->username = (string) ($user['username'] ?? '');
}
public function getUsername() : string
{
return $this->username;
}
}
```
### Object Repositories
Object Repositories are contained in the `src/Repositories` directory and must extend the
`Doctrine\SkeletonMapper\ObjectRepository\BasicObjectRepository` class.
Here is what the `UserRepository` looks like:
```php
findOneBy(['username' => $username]);
return $user;
}
}
```
### Requests
You can dynamically generate static content by using a route, controller and provider. Take this `user` route
for example:
```yaml
parameters:
doctrine.static_website.routes:
# ...
user:
path: /user/{username}.html
controller: App\Controller\UserController::user
provider: App\Requests\UserRequests::getUsers
```
Note the `App\Requests\UserRequests` class:
```php
userRepository = $userRepository;
}
public function getUsers() : RequestCollection
{
/** @var User[] $users */
$users = $this->userRepository->findAll();
$requests = [];
foreach ($users as $user) {
$requests[] = [
'username' => $user->getUsername(),
];
}
return new ArrayRequestCollection($requests);
}
}
```
For every user returned by the `getUsers` method, a `/user/{username}.html` file will be generated.
## New Flex Application
You can also get started fresh with a brand new Flex application. I would love to make this a Symfony Flex recipe
but for now you have to do it all manually but it is only a few steps.
First create a brand new Flex application:
$ composer create-project symfony/skeleton:4.1.* my-project
$ cd my-project
Now just install the `doctrine/doctrine-static-website-generator-bundle` package:
$ composer require doctrine/doctrine-static-website-generator-bundle
Add `DoctrineSkeletonMapperBundle` and `DoctrineStaticWebsiteGeneratorBundle` to the `config/bundles.php`
file to enable the bundles:
```php
['all' => true],
Doctrine\Bundle\DoctrineSkeletonMapperBundle\DoctrineSkeletonMapperBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineStaticWebsiteGeneratorBundle\DoctrineStaticWebsiteGeneratorBundle::class => ['all' => true],
];
```
Create the following directories:
$ mkdir source # source files
$ mkdir templates # twig templates
$ mkdir templates/layouts # twig layout files
Create `templates/layouts/default.html.twig` and place the following code in it:
```html
{{ site.title }}
{% block content '' %}
```
Create your first file in `source` named `index.md` and place the following content inside:
```md
Test
====
We are in a Symfony Flex application!
```
After you run `php bin/console doctrine:build-website` you will see the following file in the
`build-dev` directory:
```html
Symfony & Doctrine Static Website
Test
We are in a Symfony Flex application!
```