要点だけです。詳しくは参考のところに載せてあるリンクを参照してください。
システム内のルートの一覧を表示する
以下のコマンドでシステム内のルートの一覧を表示できます。
profilerというのは、デバッグツールによって生成されたものなので、あまり気にしなくてよいです。
$ php bin/console debug:router
-------------------------- -------- -------- ------ -----------------------------------
Name Method Scheme Host Path
-------------------------- -------- -------- ------ -----------------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
_wdt ANY ANY ANY /_wdt/{token}
_profiler_home ANY ANY ANY /_profiler/
_profiler_search ANY ANY ANY /_profiler/search
_profiler_search_bar ANY ANY ANY /_profiler/search_bar
_profiler_phpinfo ANY ANY ANY /_profiler/phpinfo
_profiler_search_results ANY ANY ANY /_profiler/{token}/search/results
_profiler_open_file ANY ANY ANY /_profiler/open
_profiler ANY ANY ANY /_profiler/{token}
_profiler_router ANY ANY ANY /_profiler/{token}/router
_profiler_exception ANY ANY ANY /_profiler/{token}/exception
_profiler_exception_css ANY ANY ANY /_profiler/{token}/exception.css
app_question_homepage ANY ANY ANY /
app_question_show ANY ANY ANY /question/{slug}
-------------------------- -------- -------- ------ -----------------------------------
- app_question_homepage
- app_question_show
というのが今回作成したページです。
現在は名前を指定していないので、コントローラのクラスとメソッド名から自動生成されています。
メソッド名が変更されたときを考えて、名前をつけておきましょう。
src/Controller/QuestionController.php
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class QuestionController extends AbstractController
{
// name=""で名前を明示的につけることができます。
/**
* @Route("/", name="app_homepage")
*/
public function homepage()
{
return $this->render('question/homepage.html.twig');
}
/**
* @Route("/question/{slug}", name="app_question_show")
*/
public function show($slug)
{
$answers = [
'Make sure your cat is sitting purrrfectly still 🤣',
'Honestly, I like furry shoes better than MY cat',
'Maybe... try saying the spell backwards?',
];
return $this->render('question/show.html.twig', [
'question' => ucwords(str_replace('-', ' ', $slug)),
'answers' => $answers,
]);
}
}
$ php bin/console debug:router
-------------------------- -------- -------- ------ -----------------------------------
Name Method Scheme Host Path
-------------------------- -------- -------- ------ -----------------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
_wdt ANY ANY ANY /_wdt/{token}
_profiler_home ANY ANY ANY /_profiler/
_profiler_search ANY ANY ANY /_profiler/search
_profiler_search_bar ANY ANY ANY /_profiler/search_bar
_profiler_phpinfo ANY ANY ANY /_profiler/phpinfo
_profiler_search_results ANY ANY ANY /_profiler/{token}/search/results
_profiler_open_file ANY ANY ANY /_profiler/open
_profiler ANY ANY ANY /_profiler/{token}
_profiler_router ANY ANY ANY /_profiler/{token}/router
_profiler_exception ANY ANY ANY /_profiler/{token}/exception
_profiler_exception_css ANY ANY ANY /_profiler/{token}/exception.css
app_homepage ANY ANY ANY /
app_question_show ANY ANY ANY /question/{slug}
-------------------------- -------- -------- ------ -----------------------------------
変更されていることが確認できました。
パスの生成
asset()
publicパスを生成する関数。CSS,JS,画像ファイル等をリンクさせるのに使います。
templates/base.html.twig
<link rel="stylesheet" href="{{asset('css/app.css')}}">
path()
パスの生成をさせるためにpath()を使います。
指定されたルートのURLを返します。
templates/base.html.twig
<a class="navbar-brand" href="{{ path('app_homepage') }}">
引数がある場合は、下記のような書き方をします。
templates/base.html.twig
<a class="q-title" href="{{ path('app_question_show', { slug: 'pausing-a-spell' }) }}"><h2>Pausing a Spell</h2></a>
参考
Generate URLs > Charming Development in Symfony 5 | SymfonyCasts