$ php --version
$ composer --version
$ mkdir symfony-vue && cd symfony-vue
$ composer create-project symfony/website-skeleton .
$ php -S localhost:9030 -t public
$ composer require annotations
.env
in the root folder of the project and then add the following API keys to it:ACCOUNT_SID=Account SID AUTH_TOKEN=AUTH TOKEN
Account SID
and AUTH TOKEN
placeholders with your actual API keys. If you are using an API that requires more keys, you can add it to this file and reference it from the Symfony app.$ composer require encore
$ yarn install
$ yarn add vue vue-loader@^15.0.11 vue-template-compiler --dev
webpack.config.js
file as follows:var Encore = require('@symfony/webpack-encore'); Encore // directory where compiled assets will be stored .setOutputPath('public/build/') // public path used by the web server to access the output path .setPublicPath('/build') .enableVueLoader() /* * ENTRY CONFIG * * Add 1 entry for each "page" of your app * (including one that's included on every page - e.g. "app") * * Each entry will result in one JavaScript file (e.g. app.js) * and one CSS file (e.g. app.css) if you JavaScript imports CSS. */ .addEntry('app', './assets/js/app.js') //.addEntry('page1', './assets/js/page1.js') // will require an extra script tag for runtime.js // but, you probably want this, unless you're building a single-page app .enableSingleRuntimeChunk() /* * FEATURE CONFIG * * Enable & configure other features below. For a full * list of features, see: * https://symfony.com/doc/current/frontend.html#adding-more-features */ .cleanupOutputBeforeBuild() .enableBuildNotifications() .enableSourceMaps(!Encore.isProduction()) // enables hashed filenames (e.g. app.abc123.css) .enableVersioning(Encore.isProduction()) ; module.exports = Encore.getWebpackConfig();
.enableVueLoader()
line so that any .vue
file can be compiled.$ yarn encore dev-server --hot
.vue
files will be detected and updated automatically.App.vue
.components
in the assets/js
folder. Then, create a new file named App.vue
in the components folder you just created. Then add a basic Vue component skeleton to the App.vue file:<template> <!-- Your HTML tags--> <h2 class="center"> Ahoy! </h2> </template> <script> // Your JavaScript code console.log("Loaded") </script> <style> /* Your styles */ .center { Text-align: center; } </style>
<template>
section, our JavaScript code in the <script>
section, and our styles will be in the <styles>
section.assets/js/app.js
file:import Vue from 'vue'; import App from './components/App'; const app = new Vue({ el: '#app', render: h => h(App) });
App.vue
component file we created. Lastly, we created a Vue instance to target an app element in the body of the HTML.HomeController.php
in the src/Controller
folder and add the code below to it:// src/Controller/HomeController.php <?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Request; class HomeController extends AbstractController { /** * @Route("/") */ public function index() { return $this->render('index.html.twig'); } }
@Route("/")
, which will render an index.html.twig
file in the templates
folder.base.html.twig
file in the templates folder, which is the base layout with the markup below:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>{% block title %}Welcome!{% endblock %}</title> {% block stylesheets %} {{ encore_entry_link_tags('app') }} {% endblock %} </head> <body> {% block body %}{% endblock %} {% block javascripts %} {{ encore_entry_script_tags('app') }} {% endblock %} </body> </html>
index.html.twig
in the templates
folder, and add the markup below to it:{% extends 'base.html.twig' %} {% block title %} Welcome !{% endblock %} {% block body %} <div class="example-wrapper" id="app"> <app></app> </div> {% endblock %}
<app></app>
tag. This is the App.vue
component we included in the assets/js/app.js
file, which is going to render the part where Vue will watch over.$ composer require twilio/sdk
Onwuka Gideon is a Full-stack Software developer with years of experience developing web applications. You can reach him on Twitter or Github.