記事作成の背景
ページ遷移の備忘録して残します。
完成イメージ
こんな感じで、ページを遷移させるサンプルです。
↑↓
templates/conference/index.html.twg
{% extends 'base.html.twig' %}
{% block title %}Hello ConferenceController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code><a href="{{ 'C:/Users/pgrfr/Desktop/hellosymfony6/src/Controller/ConferenceController.php'|file_link(0) }}">src/Controller/ConferenceController.php</a></code></li>
<li>Your template at <code><a href="{{ 'C:/Users/pgrfr/Desktop/hellosymfony6/templates/conference/index.html.twig'|file_link(0) }}">templates/conference/index.html.twig</a></code></li>
</ul>
<div>
{{welcome_name}}
</div>
<div>
<a href="/conference/second">つぎへ</a>
</div>
</div>
{% endblock %}
templates/conference/second.html.twg
<h1>{{second_page}}</h1>
<h2>
<a href="/conference">戻る</a>
</h2>
src/Controller/ConferenceController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;//追加
use App\Entity\Product; // Productエンティティの名前空間を正しく指定
class ConferenceController extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager){
$this->entityManager = $entityManager;
}
#[Route('/conference', name: 'app_conference')]
public function index(): Response
{
$conferencedata = 'ConferenceController';
$welcomename = '大谷';
$id=1;
$memberlist = $this->entityManager->getRepository(Product::class)->find($id);
//dd($memberlist->getName());
$welcomename = $memberlist->getName();
return $this->render('conference/index.html.twig', [
'controller_name' => $conferencedata,
'welcome_name'=>$welcomename
]);
}
#[Route('/conference/second',name:'app_conference_second')]
public function secondPage():Response
{
$secondData = '遷移後のページ';
return $this->render('conference/second.html.twig', [
'second_page' => $secondData
]);
}
}
以上です。
こんなかんじでページ遷移させることができました、。