Finished
This commit is contained in:
parent
ea41c3cccc
commit
ebeafaeeed
81
src/Controller/MissionController.php
Normal file
81
src/Controller/MissionController.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Mission;
|
||||
use App\Form\MissionType;
|
||||
use App\Repository\MissionRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/mission', name: 'mission')]
|
||||
final class MissionController extends AbstractController
|
||||
{
|
||||
#[Route(name: '_index', methods: ['GET'])]
|
||||
public function index(MissionRepository $missionRepository): Response
|
||||
{
|
||||
return $this->render('mission/index.html.twig', [
|
||||
'missions' => $missionRepository->findAll(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: '_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$mission = new Mission();
|
||||
$form = $this->createForm(MissionType::class, $mission);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($mission);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('mission/new.html.twig', [
|
||||
'mission' => $mission,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: '_show', methods: ['GET'])]
|
||||
public function show(Mission $mission): Response
|
||||
{
|
||||
return $this->render('mission/show.html.twig', [
|
||||
'mission' => $mission,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])]
|
||||
public function edit(Request $request, Mission $mission, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$form = $this->createForm(MissionType::class, $mission);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('mission/edit.html.twig', [
|
||||
'mission' => $mission,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: '_delete', methods: ['POST'])]
|
||||
public function delete(Request $request, Mission $mission, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$mission->getId(), $request->getPayload()->getString('_token'))) {
|
||||
$entityManager->remove($mission);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
25
src/Form/MissionType.php
Normal file
25
src/Form/MissionType.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Mission;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class MissionType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('label')
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Mission::class,
|
||||
]);
|
||||
}
|
||||
}
|
4
templates/mission/_delete_form.html.twig
Normal file
4
templates/mission/_delete_form.html.twig
Normal file
@ -0,0 +1,4 @@
|
||||
<form method="post" action="{{ path('_delete', {'id': mission.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
|
||||
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ mission.id) }}">
|
||||
<button class="btn">Delete</button>
|
||||
</form>
|
4
templates/mission/_form.html.twig
Normal file
4
templates/mission/_form.html.twig
Normal file
@ -0,0 +1,4 @@
|
||||
{{ form_start(form) }}
|
||||
{{ form_widget(form) }}
|
||||
<button class="btn">{{ button_label|default('Save') }}</button>
|
||||
{{ form_end(form) }}
|
13
templates/mission/edit.html.twig
Normal file
13
templates/mission/edit.html.twig
Normal file
@ -0,0 +1,13 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Edit Mission{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Edit Mission</h1>
|
||||
|
||||
{{ include('mission/_form.html.twig', {'button_label': 'Update'}) }}
|
||||
|
||||
<a href="{{ path('app_mission_index') }}">back to list</a>
|
||||
|
||||
{{ include('mission/_delete_form.html.twig') }}
|
||||
{% endblock %}
|
35
templates/mission/index.html.twig
Normal file
35
templates/mission/index.html.twig
Normal file
@ -0,0 +1,35 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Mission index{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Mission index</h1>
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Label</th>
|
||||
<th>actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for mission in missions %}
|
||||
<tr>
|
||||
<td>{{ mission.id }}</td>
|
||||
<td>{{ mission.label }}</td>
|
||||
<td>
|
||||
<a href="{{ path('mission_show', {'id': mission.id}) }}">show</a>
|
||||
<a href="{{ path('mission_edit', {'id': mission.id}) }}">edit</a>
|
||||
</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="3">no records found</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<a href="{{ path('mission_new') }}">Create new</a>
|
||||
{% endblock %}
|
11
templates/mission/new.html.twig
Normal file
11
templates/mission/new.html.twig
Normal file
@ -0,0 +1,11 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}New Mission{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Create new Mission</h1>
|
||||
|
||||
{{ include('mission/_form.html.twig') }}
|
||||
|
||||
<a href="{{ path('mission_index') }}">back to list</a>
|
||||
{% endblock %}
|
26
templates/mission/show.html.twig
Normal file
26
templates/mission/show.html.twig
Normal file
@ -0,0 +1,26 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Mission{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Mission</h1>
|
||||
|
||||
<table class="table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<td>{{ mission.id }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Label</th>
|
||||
<td>{{ mission.label }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<a href="{{ path('_index') }}">back to list</a>
|
||||
|
||||
<a href="{{ path('_edit', {'id': mission.id}) }}">edit</a>
|
||||
|
||||
{{ include('mission/_delete_form.html.twig') }}
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user