본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Tailwind CSS Symfony (PHP) 시작하기

원문 보기 위키 갱신

Symfony는 PHP 생태계의 대표적인 웹 애플리케이션 프레임워크예요. 50개 이상의 독립형 PHP 컴포넌트와 MVC 프레임워크를 제공해서 복잡한 애플리케이션, 웹사이트 페이지, API 서비스까지 만들 수 있어요. 이 가이드에서 새 Symfony 프로젝트를 만들고 Tailwind CSS를 설치·구성한 다음 Flowbite와 오픈소스 UI 컴포넌트를 활용해서 웹사이트를 더 빠르게 만드는 방법을 배워볼게요.

출처: 문서

본문

요구사항 (Requirements)

시작하기 전에 로컬 환경에 PHP (v8.1 이상), Composer, Node.js가 설치되어 있어야 해요. 그래야 Symfony, Tailwind CSS, Flowbite를 제대로 설정하고 구성할 수 있어요.

Symfony 프로젝트 만들기 (Create a Symfony project)

새 Symfony 프로젝트를 만드는 가장 간단한 방법은 공식 Symfony CLI 인스톨러를 사용하는 거예요. macOS와 Linux에서는 Homebrew로 쉽게 설치할 수 있어요:

brew install symfony-cli/tap/symfony-cli

Windows에서는 Scoop을 사용해 Symfony CLI를 설치할 수도 있어요:

scoop install symfony-cli

CLI를 준비했으면 새 프로젝트를 만들어 볼게요. 터미널에서 다음 명령으로 새 프로젝트를 만듭니다:

symfony new --webapp flowbite-app

이 명령은 새 Symfony 프로젝트 설치가 포함된 새 폴더를 만들어요.

새로 만든 프로젝트 폴더로 작업 디렉터리를 변경해요:

cd flowbite-app

프런트엔드 통합을 위해 추천되는 Symfony Webpack Encore 번들을 Composer로 설치하고 기본 의존성을 제거해요:

composer remove symfony/ux-turbo symfony/asset-mapper symfony/stimulus-bundle
composer require symfony/webpack-encore-bundle

로컬 웹 서버를 시작하려면 터미널에서 다음 명령을 실행해요:

symfony server:start

이 명령으로 웹 애플리케이션을 브라우저에서 http://localhost:8000으로 접속할 수 있어요.

서버에 배포할 때는 Symfony에서 기본 제공하는 Deployer, Ansistrano, Fabric 같은 공식 프로덕션 배포 도구와 번들러를 확인해 보는 걸 추천해요.

Tailwind CSS 설치 (Install Tailwind CSS)

Tailwind CSS는 사전 구성된 클래스를 기반으로 HTML 안에서 직접 스타일을 코딩할 수 있게 해 주는 인기 있는 오픈소스 utility-first CSS 프레임워크로, UI 프런트엔드 개발 속도를 높여줘요.

NPM으로 Tailwind CSS와 PostCSS를 설치해요:

npm install tailwindcss @tailwindcss/postcss postcss postcss-loader --save-dev

기본 ./assets/styles/app.css 파일 안에 Tailwind 기본 지시문을 import 해요:

@import "tailwindcss";

Webpack 설정 파일에 PostCSS 로더 플러그인을 추가해요:

// webpack.config.js
Encore
    // other plugins

    .enablePostCssLoader()

    // ... more plugins

루트 폴더에 새 postcss.config.mjs 파일을 만들고 다음 구성을 추가해요:

export default {
  plugins: {
    "@tailwindcss/postcss": {},
  },
};

Webpack으로 프런트엔드 에셋을 컴파일해요:

npm run watch

Symfony 라우트 파일을 구성해서 새 홈페이지 엔트리 파일을 만들어요:

// ./config/routes.yml

index:
    path: /
    controller: App\Controller\DefaultController::index

새 DefaultController를 만들고 새 Twig 템플릿 파일의 경로를 설정해요. App\Controller\DefaultController를 만들고 index() 메서드 안에서 templates/index.html.twig 템플릿을 렌더링해요.

Flowbite 설치 (Install Flowbite)

Flowbite는 Tailwind CSS의 유틸리티 클래스 위에 구축된 무료·인기 오픈소스 UI 컴포넌트 라이브러리예요. 드롭다운, 내비게이션 바, 모달 같은 인터랙티브 UI 요소와 웹사이트 섹션, 템플릿, 플러그인, 도구 생태계도 함께 제공해서 웹사이트를 더 빠르게 만들 수 있어요.

NPM으로 Flowbite를 의존성으로 설치해요:

npm install flowbite --save

메인 app.css CSS 파일 안에 Flowbite의 기본 테마 변수를 import 해요:

@import "flowbite/src/themes/default";

CSS 파일에 Flowbite 플러그인 파일을 import 해요:

@plugin "flowbite/plugin";

CSS에서 Flowbite의 소스 파일을 구성해요:

@source "../../node_modules/flowbite";

./assets/app.js 파일 안에서 Flowbite 패키지를 import 하면 UI 컴포넌트의 인터랙티브 기능이 활성화돼요:

/*
 * Welcome to your app's main JavaScript file!
 *
 * We recommend including the built version of this JavaScript file
 * (and its CSS file) in your base layout (base.html.twig).
 */

// any CSS you import will output into a single css file (app.css in this case)
import './styles/app.css';

// start the Stimulus application
import './bootstrap';

// enable the interactive UI components from Flowbite
import 'flowbite';

이렇게 하면 드롭다운, 모달, 내비게이션 바 등 모든 인터랙티브·동적 UI 컴포넌트가 바로 동작해요. Webpack이 최종 자바스크립트 파일을 자동으로 번들해서 속도와 성능을 높여요.

Flowbite Quickstart 가이드에서 CDN, data 속성 인터페이스, TypeScript 활용, 프로그래매틱 방식 등 JS 인터랙티브 기능을 설정하는 다양한 방법을 확인할 수 있어요.

Symfony UX Turbo

Flowbite는 flowbite.turbo.js 파일을 import 하면 turbo load를 지원하는 커스텀 이벤트 리스너를 제공해요. Webpack Encore와 함께 인터랙티브 컴포넌트를 구동하는 자바스크립트 파일을 통합하는 방법은 관련 가이드를 확인해 보세요.

Symfony UX Turbo를 사용한다면 load 대신 turbo:load 이벤트 리스너를 지원하는 Flowbite 버전을 import 해야 해요. ./assets/app.js 파일에 아래 줄을 추가해요:

/*
 * Welcome to your app's main JavaScript file!
 *
 * We recommend including the built version of this JavaScript file
 * (and its CSS file) in your base layout (base.html.twig).
 */

// any CSS you import will output into a single css file (app.css in this case)
import './styles/app.css';

// start the Stimulus application
import './bootstrap';

// enable the interactive UI components from Flowbite with Turbo
import 'flowbite/dist/flowbite.turbo.js';

Flowbite 컴포넌트 (Flowbite components)

이제 Symfony 프로젝트에 모든 기술이 성공적으로 설정됐으니 Tailwind CSS의 유틸리티 클래스, Flowbite의 사전 제작 UI 컴포넌트, Symfony의 프레임워크 구조를 함께 활용해서 웹사이트를 빠르고 효율적으로 만들 수 있어요.

헤더의 로그인 버튼을 클릭할 때 Flowbite 라이브러리의 간단한 모달 인증 컴포넌트를 추가해 볼게요:

<!-- Modal toggle -->
<button data-modal-target="authentication-modal" data-modal-toggle="authentication-modal" class="block text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" type="button">
  Toggle modal
</button>

<!-- Main modal -->
<div id="authentication-modal" tabindex="-1" aria-hidden="true" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full">
    <div class="relative p-4 w-full max-w-md max-h-full">
        <!-- Modal content -->
        <div class="relative bg-white rounded-lg shadow-sm dark:bg-gray-700">
            <!-- Modal header -->
            <div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600 border-gray-200">
                <h3 class="text-xl font-semibold text-gray-900 dark:text-white">
                    Sign in to our platform
                </h3>
                <button type="button" class="end-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="authentication-modal">
                    <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
                    </svg>
                    <span class="sr-only">Close modal</span>
                </button>
            </div>
            <!-- Modal body -->
            <div class="p-4 md:p-5">
                <form class="space-y-4" action="#">
                    <div>
                        <label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your email</label>
                        <input type="email" name="email" id="email" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white" placeholder="name@company.com" required />
                    </div>
                    <div>
                        <label for="password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your password</label>
                        <input type="password" name="password" id="password" placeholder="••••••••" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white" required />
                    </div>
                    <div class="flex justify-between">
                        <div class="flex items-start">
                            <div class="flex items-center h-5">
                                <input id="remember" type="checkbox" value="" class="w-4 h-4 border border-gray-300 rounded-sm bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-600 dark:border-gray-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 dark:focus:ring-offset-gray-800" required />
                            </div>
                            <label for="remember" class="ms-2 text-sm font-medium text-gray-900 dark:text-gray-300">Remember me</label>
                        </div>
                        <a href="#" class="text-sm text-blue-700 hover:underline dark:text-blue-500">Lost Password?</a>
                    </div>
                    <button type="submit" class="w-full text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Login to your account</button>
                    <div class="text-sm font-medium text-gray-500 dark:text-gray-300">
                        Not registered? <a href="#" class="text-blue-700 hover:underline dark:text-blue-500">Create account</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

이 모달 내부에서 data-modal-target, data-modal-toggle, data-modal-hide 같은 data 속성들을 사용해서 열고 닫는 인터랙티브 동작을 제어할 수 있어요.

Flowbite Bundle

Symfony 전용 통합 폼 테마와 특정 폼 타입을 제공하는 비공식 Flowbite Bundle도 사용할 수 있어요.

Symfony 스타터 프로젝트 (Symfony starter project)

Flowbite의 훌륭한 오픈소스 커뮤니티가 Symfony + Tailwind CSS 스타터 프로젝트를 만들었어요. 이 통합 가이드를 건너뛰고 이 프로젝트를 미래의 Symfony, Tailwind CSS, Flowbite 프로젝트의 시작점으로 사용할 수 있어요.

더 알아보기 (Learn more)

  • Symfony 프로젝트에 Tailwind CSS는 @import "tailwindcss"로, Flowbite는 npm install flowbite --save로 손쉽게 추가할 수 있어요.
  • ./assets/app.js에서 import 'flowbite'를 추가하면 드롭다운·모달·내비게이션 바 등 인터랙티브 컴포넌트가 바로 동작해요.
  • Symfony UX Turbo를 쓸 때는 flowbite/dist/flowbite.turbo.js를 import 해야 turbo:load 이벤트를 지원해요.
  • 코드와 클래스는 원문 그대로 유지해요.