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 3 years have passed since last update.

Symfony4で設定ファイルを定義してみる

Last updated at Posted at 2021-07-04

Symfony4でパラメーターなどを一つの設定ファイルで定義したかったので、その内容をまとめる。

以下に設定ファイルを作成する

config/parameter.yaml
user:
    age: 8

BaseContorollerを作成して、各Controllerはそこから継承するようにする

src/Controller/BaseController.php
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

abstract class BaseController extends AbstractController
{
    // 後ほどメソッドなどを定義していく
}

src/Controller/UserController.php
diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php
index de63605..2450323 100644
--- a/src/Controller/UserController.php
+++ b/src/Controller/UserController.php
@@ -2,16 +2,16 @@

 namespace App\Controller;

+use App\Controller\BaseController;
 use App\Entity\User;
 use App\Service\UserService;
 use App\Form\UserType;
-use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

-class UserController extends AbstractController
+class UserController extends BaseController
 {
     /**
      * @Route("/user", name="users")
     public function index(UserService $user_service): Response
     {


BaseConrollerのコンストラクターで、conf情報を持つ変数を設定する(ついでにその変数を取得するメソッドも定義する)

src/Controller/BaseController.php
use Symfony\Component\Yaml\Yaml;

abstract class BaseController extends AbstractController
{
    protected $yaml_info = '';

    function __construct() {
        $yaml_file = file_get_contents("../config/parameter.yaml"); // 読み込むyamlファイルを指定します。
        $this->yaml_info = Yaml::parse($yaml_file);
    }

    public function getServiceConfig() {
        return $this->yaml_info;
    }
}

各コントローラーで設定ファイルの内容を取得できるか確認してみる。

src/Controller/UserController.php
class UserController extends BaseController
{
    /**
     * @Route("/user", name="users")
     */
    public function index(UserService $user_service): Response
    {
        $users = $user_service->getUsers();

+        $tmp = $this->getServiceConfig();
+        var_dump($tmp); // 結果を出力します。
+        exit;
    
        return $this->render('user/index.html.twig', [
            'title' => 'ユーザー一覧',
            'data' => $users,
        ]);
    }

⇒取得できていそうだったので、問題なし。

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?