0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PHP】Symfonyで画面間を遷移させるための方法

Posted at

記事作成の背景

ページ遷移の備忘録して残します。

完成イメージ

こんな感じで、ページを遷移させるサンプルです。

サンプル.png

↑↓

遷移.png

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
        ]);
    }
}

以上です。
こんなかんじでページ遷移させることができました、。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?