Created new page do display single ship and linked this from homepage with names routes.
This commit is contained in:
24
src/Controller/StarshipController.php
Normal file
24
src/Controller/StarshipController.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Repository\StarshipRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class StarshipController extends AbstractController
|
||||
{
|
||||
#[Route('/starships/{id<\d+>}', name: 'app_starship_show')]
|
||||
public function show(int $id, StarshipRepository $repository): Response
|
||||
{
|
||||
$ship = $repository->findOne($id);
|
||||
if (null === $ship) {
|
||||
throw $this->createNotFoundException('Starship not found');
|
||||
}
|
||||
|
||||
return $this->render('starship/show.html.twig', [
|
||||
'ship' => $ship,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,4 +2,8 @@
|
||||
|
||||
{% block body %}
|
||||
{{ variable }}
|
||||
|
||||
<a href="{{ path('app_starship_show', {'id' : 1}) }}">Ship 01</a>
|
||||
<a href="{{ path('app_starship_show', {'id' : 2}) }}">Ship 02</a>
|
||||
<a href="{{ path('app_starship_show', {'id' : 3}) }}">Ship 03 (not found)</a>
|
||||
{% endblock %}
|
||||
|
||||
20
templates/starship/show.html.twig
Normal file
20
templates/starship/show.html.twig
Normal file
@@ -0,0 +1,20 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}{{ ship.name }}{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>{{ ship.name }}</h1>
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Class</th>
|
||||
<td>{{ ship.class }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Captain</th>
|
||||
<td>{{ ship.captain }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user