add documentation

This commit is contained in:
Simon Vieille 2021-05-31 23:23:15 +02:00
parent bd4bc81e9d
commit 0413a3d677
5 changed files with 219 additions and 132 deletions

98
docs/controller.md Normal file
View file

@ -0,0 +1,98 @@
# Controller
The default controller of a node is `App\Core\Controller\Site\PageController::show`.
`PageController` extends `Symfony\Bundle\FrameworkBundle\Controller\AbstractController` and implements very basic features: a Response builder which retrieves the good template and injects variables to the view.
To create a custom controller, do this way:
```
// src/Controller/MyController.php
namespace App\Controller;
use App\Core\Controller\Site\PageController;
class MyController extends PageController
{
public function myAction()
{
if (!$this->siteRequest->getPage()) {
throw $this->createNotFoundException();
}
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
// view datas
]);
}
}
```
Then edit `config/packages/app.yaml` and add your controller:
```
core:
site:
controllers:
- {name: 'My action', action: 'App\Controller\MyController::myAction'}
```
If your controller represents entities and if the associated node is visible in the sitemap, you can use a `App\Core\Annotation\UrlGenerator` in annotations and implement a generator. See the example below.
```
// src/Controller/MyController.php
namespace App\Controller;
use App\Core\Annotation\UrlGenerator;
use App\Core\Controller\Site\PageController;
use App\UrlGenerator\MyEntityGenerator;
class MyController extends PageController
{
/**
* @UrlGenerator(service=MyEntityGenerator::class, method="myAction")
*/
public function myAction(MyEntity $entity)
{
// do stuff
}
}
```
```
// src/UrlGenerator/MyEntityGenerator
namespace App\UrlGenerator;
use App\Repository\MyEntityRepositoryQuery;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class MyEntityGenerator
{
protected MyEntityRepositoryQuery $query;
protected UrlGeneratorInterface $urlGenerator;
public function __construct(MyEntityRepositoryQuery $query, UrlGeneratorInterface $urlGenerator)
{
$this->query = $query;
$this->urlGenerator = $urlGenerator;
}
public function myAction(): array
{
$entities = $this->query->create()->find();
$urls = [];
foreach ($entities as $entity) {
$urls[] = $this->urlGenerator->generate(
'the_route_name',
[
'entity' => $entity->getId(),
],
UrlGeneratorInterface::ABSOLUTE_URL
);
}
return $urls;
}
}
```

0
docs/template.md Normal file
View file

View file

@ -12,138 +12,6 @@ To create or update a node, click on "Edit" or the sign "+". The basic informati
The content tab allow you to define an optional `Page` to associate to the node. You can also define that the node is an alias of another node.
A page only represents the content and a way to display it by setting a template.
To add a page in the list, edit `config/packages/app.yaml`:
```
core:
site:
pages:
App\Entity\Page\SimplePage:
name: 'Simple page'
templates:
- {name: "Default", file: "page/simple/default.html.twig"}
App\Entity\Page\YourPage:
name: 'Your page'
templates:
- {name: "Template 1", file: "page/your_page/template1.html.twig"}
App\Entity\Page\SimplePage:
name: 'Page simple'
templates:
- {name: "Template", file: "page/simple/template.html.twig"}
App\Entity\Page\SimplePage:
name: 'Page simple'
templates:
- {name: "Template", file: "page/simple/template.html.twig"}
App\Entity\Page\SimplePage:
name: 'Page simple'
templates:
- {name: "Template", file: "page/simple/template.html.twig"}
App\Entity\Page\SimplePage:
name: 'Page simple'
templates:
- {name: "Template", file: "page/simple/template.html.twig"}
- {name: "Template 2", file: "page/your_page/template2.html.twig"}
```
### Page
A page is a doctrine entity that contains blocks and a way to edit them. For each element defined in `buildForm`, you must add a getter and a setter. See the example below.
```
// src/Entity/Page/YourPage.php
namespace App\Entity\Page;
use App\Core\Entity\Site\Page\Block;
use App\Core\Entity\Site\Page\Page;
use App\Core\Form\Site\Page\TextBlockType;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @ORM\Entity
*/
class YourPage extends Page
{
public function buildForm(FormBuilderInterface $builder)
{
$builder->add(
'myBlock',
TextBlockType::class,
[
'label' => 'My block',
'options' => [
// options given to the sub form
],
]
);
}
public function setTitle(Block $block)
{
return $this->setBlock($block);
}
public function getTitle(): Block
{
return $this->getBlock('myBlock');
}
}
```
#### TextBlockType
`App\Core\Form\Site\Page\TextBlockType` will render a symfony `TextType`.
#### TextareaBlockType
`App\Core\Form\Site\Page\TextareaBlockType` will render a symfony `TextareaType`.
#### FileBlockType
`App\Core\Form\Site\Page\FileBlockType` will render a symfony `FileType` with a download link.
In the getter, you must specify the block:
```
use App\Core\Entity\Site\Page\FileBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', FileBlock::class);
}
```
#### ImageBlockType
`App\Core\Form\Site\Page\ImageBlockType` will render a symfony `FileType` with a preview of the image.
In the getter, you must specify the block:
```
use App\Core\Entity\Site\Page\FileBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', FileBlock::class);
}
```
#### CollectionBlockType
`App\Core\Form\Site\Page\CollectionBlockType` will a render a symfony `CollectionType ` with availabity to add and remove elements.
```
use App\Core\Entity\Site\Page\CollectionBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', CollectionBlock::class);
}
```
## Routing
The routing tab is very important. It allows you to define all parameters related to:

118
docs/tree/page.md Normal file
View file

@ -0,0 +1,118 @@
# Page
A page is a doctrine entity that contains blocks and form builder. For each element defined in `buildForm`, you must add a getter and a setter. See the example below.
```
// src/Entity/Page/YourPage.php
namespace App\Entity\Page;
use App\Core\Entity\Site\Page\Block;
use App\Core\Entity\Site\Page\Page;
use App\Core\Form\Site\Page\TextBlockType;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @ORM\Entity
*/
class YourPage extends Page
{
public function buildForm(FormBuilderInterface $builder)
{
$builder->add(
'myBlock',
TextBlockType::class,
[
'label' => 'My block',
'options' => [
// options given to the sub form
],
]
);
// ...
}
public function setMyBlock(Block $block)
{
return $this->setBlock($block);
}
public function getMyBlock(): Block
{
return $this->getBlock('myBlock');
}
// ...
}
```
Then edit `config/packages/app.yaml` and add your page:
```
core:
site:
pages:
App\Entity\Page\SimplePage:
name: 'Simple page'
templates:
- {name: "Default", file: "page/simple/default.html.twig"}
App\Entity\Page\YourPage:
name: 'Your page'
templates:
- {name: "Template 1", file: "page/your_page/template1.html.twig"}
- {name: "Template 2", file: "page/your_page/template2.html.twig"}
```
## Blocks
### TextBlockType
`App\Core\Form\Site\Page\TextBlockType` will render a symfony `TextType`.
### TextareaBlockType
`App\Core\Form\Site\Page\TextareaBlockType` will render a symfony `TextareaType`.
### FileBlockType
`App\Core\Form\Site\Page\FileBlockType` will render a symfony `FileType` with a download link.
In the getter, you must specify the block:
```
use App\Core\Entity\Site\Page\FileBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', FileBlock::class);
}
```
### ImageBlockType
`App\Core\Form\Site\Page\ImageBlockType` will render a symfony `FileType` with a preview of the image.
In the getter, you must specify the block:
```
use App\Core\Entity\Site\Page\FileBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', FileBlock::class);
}
```
### CollectionBlockType
`App\Core\Form\Site\Page\CollectionBlockType` will a render a symfony `CollectionType ` with availabity to add and remove elements.
```
use App\Core\Entity\Site\Page\CollectionBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', CollectionBlock::class);
}
```

View file

@ -11,6 +11,9 @@ nav:
- Navigation: tree/navigation.md
- Menu: tree/menu.md
- Node: tree/node.md
- Page: tree/page.md
- Controller: controller.md
- Template: template.md
- Entities:
- 'Entity Manager': entities/em.md
- 'Repository Query': entities/query.md