Advanced Integration with VueJS
Let's suppose that you want to add on a new administration section in Devise or build in your own VueJS components so that Webpack can do it's thing and optimize your build? Well, we've set it up so that you can do that very easily. This Guide assumes you've setup a base install of Devise.
What we're going to setup
Devise comes in two parts:
The PHP API that ties into Laravel (Devise CMS)
The VueJS frontend that gives you and your content managers a way to interact with that API (Devise Interface)
What we're going to do here is setup a way for you to recompile the frontend piece with your own Javascript. This is where Devise really shines because it leverages the power of Vue CLI and Webpack to make your bundles as small as possible, chunked out so they are lazy loaded, and compile CSS frameworks like TailwindCSS. So let's get started. Again, this Guide will assume you've setup a base install of Devise so if you haven't done those steps get those knocked out and then come back here .
Prerequisites: Setup Vue CLI
Vue CLI is an amazing utility that will do our compiling for us. Go ahead and follow their install guide and once you're done you should be able to run vue ui
from your command line and see the UI.
Create a new Vue project
On the root of your Laravel project create a new Vue project by clicking the "Create" button. We like to name our projects something like "projectname-interface". During the creation you've got a few options but you're going to want to include, at minimum:
Vue Router
Vuex
Babel
Typically, we also include our SASS (or LESS) and eslint in our configuration so that we can enjoy the benefits of hot module reloading and linting.
Add dependencies and install Devise Interface
Once in your project click on the "dependencies" tab and add the following as dependencies.
devisephp-interface
vuex-router-sync
axios
Or add them via the command line with:
yarn add devisephp-interface vuex-router-sync axios
Add other dev dependencies
Again, you can add them with the "dev-dependencies" UI in Vue CLI
tailwindcss
webpack-assets-manifest
Or via the command line:
yarn add -D tailwindcss webpack-assets-manifest
Initialize TailwindCSS
From the root of your interface directory:
yarn tailwind init tailwind.js
Create vue.config.js
Inside your new vue-cli project create a vue.config.js file and place the following:
Be sure to change the developmentUrl to your development domain
Create an event bus
In your interface src directory next to your main.js file create event-bus.js
and put in the following:
Modify your main.js file
Your main.js file is the "entry" to your application. It's where everything starts. Here we are going to initialize VueJS and add Devise Interface as a plugin.
Modify router.js
In your interface folder's src
directory modify router.js to be the following:
Modify your layout
We're almost there! We need to modify the layout of our blade file to accommodate for hot module reloading. If you followed the basic installation it will be located in /resources/views/layouts/main.blade.php
. It needs to look something like the following:
Update package.json script
Update the scripts
section of package.json with the following:
Create HOT file
Create a directory in your Vue CLI project root called hmr
and a file within it called hot
with the following contents:
Last Minute things before you build or serve
Create the public/app directory
In the root of your project in the public directory (Laravel's public directory) make sure you create an app directory called... you guessed it.... app
Clear the output directory setting
Vue CLI has a default setting in the "Output Directory" of the parameters in the build script. To change this click on "Tasks", then "Build", then "Parameters" and clear the "Output Directory" setting so that nothing is in it.
Build!
You did great! Head to the Tasks tab on the Vue CLI UI, click "Build", and then the "Run" button. If you have no errors you should see a bunch of new files in /public/app
(or wherever you configured your output to be in your vue.config.js).
When you visit your site if you only see a white screen make sure you have logged in at http://your-project.test/login
and logged in.
Troubleshooting
First, there are many ways to accomplish what we are doing here and every project is going to have individual needs. If you need a little guidance on how to set things up take a look at the DevisePHP Marketing Source. That project is a great baseline to refer to. Of course, feel free to submit an issue and we can try and give you a hand.
Common Issues
Before anything else take a look on the "output" tab on the build screen and really read the error. Typically, I'll forget to add a dependency or publish my tailwind.js config file.
Last updated