murph-doc/docs/controller.md

111 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2021-05-31 23:23:15 +02:00
# Controller
2021-06-04 10:48:41 +02:00
## Controller
2021-05-31 23:23:15 +02:00
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:
2023-02-09 21:50:06 +01:00
```php-inline title="src/Controller/MyController.php"
2021-05-31 23:23:15 +02:00
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:
2023-02-09 21:50:06 +01:00
```yaml
2021-05-31 23:23:15 +02:00
core:
site:
controllers:
- {name: 'My action', action: 'App\Controller\MyController::myAction'}
```
2021-06-04 10:48:41 +02:00
## UrlGenerator
2021-05-31 23:23:15 +02:00
2021-06-04 10:48:41 +02:00
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.
2021-05-31 23:23:15 +02:00
2023-02-09 21:50:06 +01:00
```php-inline title="src/UrlGenerator/MyEntityGenerator"
2021-05-31 23:23:15 +02:00
namespace App\UrlGenerator;
2021-06-04 10:48:41 +02:00
use App\Core\Entity\Site\Node;
2021-05-31 23:23:15 +02:00
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;
}
2021-06-04 10:48:41 +02:00
public function myActionGenerator(Node $node, array $options): array
2021-05-31 23:23:15 +02:00
{
$entities = $this->query->create()->find();
$urls = [];
foreach ($entities as $entity) {
$urls[] = $this->urlGenerator->generate(
2021-06-04 10:48:41 +02:00
$node->getRouteName(),
2021-05-31 23:23:15 +02:00
[
'entity' => $entity->getId(),
2022-05-14 22:17:28 +02:00
'_domain' => $options['_domain'],
2021-05-31 23:23:15 +02:00
],
UrlGeneratorInterface::ABSOLUTE_URL
);
}
return $urls;
}
}
```
2021-06-04 10:48:41 +02:00
Then, the have to annotate the controller like this (note: `options` is optional):
2023-02-09 21:50:06 +01:00
```php-inline title="src/Controller/MyController.php"
2021-06-04 10:48:41 +02:00
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: 'myActionGenerator', options=[])]
2021-06-04 10:48:41 +02:00
public function myAction(MyEntity $entity)
{
// do stuff
}
}
```
2021-10-20 22:22:23 +02:00
Finally, update `config/services.yaml`:
2023-02-09 21:50:06 +01:00
```yaml
2021-10-20 22:22:23 +02:00
services:
# ...
App\UrlGenerator\MyEntityGenerator:
public: true
```