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?

More than 5 years have passed since last update.

Symfonyの構造 annotation

Posted at

annotationは標準機能ではないが、rootディレクトリをYAMLで作るよりも簡単なのでしばしば使われます。

まずComposerをinstallしましょう。

brew install composer

次にannotationをインストールします。

composer require annotations

annotationを使ってみる

annotationはroutes.yamlをphpコードの中に表現するやり方なので、config/routes.yamlの中身を全てコメントアウトします。
そして、下記を書き換えます。

QuestionController.php
<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class QuestionController
{
    
    /**
     * @Route("/")
     */
    public function homepage()
    {
        return new Response('Hello World');
    }

    /**
     * @Route("/test/{slug}")
     */
    public function show($slug)
    {
        return new Response(sprintf(ucwords(str_replace('-', ' ', $slug))));
    }
}

下記を書くことで、slugの部分をURLで指定することができます。
https://localhost:8000/test/aiuoe
を指定すると
生成されたページにAiuoeと表示されます。

    /**
     * @Route("/test/{slug}")
     */

参考

Annotation & Wildcard Routes > Charming Development in Symfony 5 | SymfonyCasts

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?