Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

willbrowning's avatar

Setting up Vue-Router with Spark 3.0

Hi,

I was waiting for Spark to be updated to ship with vue.js 2, now Spark 3.0 has been released which does.

I'm relatively new to Vue.js so I have used this starter template to get Vue-Router working with Laravel. However I have tried to do this with Spark 3.0 but cannot seem to get it working correctly.

Since the structure of Spark and the way variables appear to be declared for Vue is slightly different in Spark I'm not sure what to add to app.js and how to handle route collection using Vue.

I've added this to the end of the web routes file, however I believe all Spark routes are in the vendor folder, are these called before the routes in the web file?

// vue-router will handle any other requests
Route::get('/{vue_capture?}', function () {
    return view('welcome');
})->where('vue_capture', '[\/\w\.-]*')->middleware('auth');

What is the best way to set up Vue-Router with Spark 3.0 so that all Laravel routes are handled first and then any others are handled by Vue.

Any help would be greatly appreciated.

0 likes
8 replies
willbrowning's avatar

Okay, I've managed to get the vue routing to work, however now I have a problem with calling the api routes, since it appears that the api routes must be checked after the web routes because api calls are no longer working after I have placed the vue_capture route at the end of the web routes file. (When I remove the vue_capture route, api calls work but then of course vue-router does not).

How can I ensure that vue handles routes only after all other Laravel routes have been checked including api routes?

My app.js file looks like this:

require('spark-bootstrap');

require('./components/bootstrap');

import Example from './components/Example.vue';
import Foo from './components/Foo.vue';
import Bar from './components/Bar.vue';

import VueRouter from 'vue-router';

Vue.use(VueRouter);

const router = new VueRouter({
    mode: 'history',
    base: __dirname,
    routes: [
        { path: '/', component: Example },
        { path: '/foo', component: Foo },
        { path: '/bar', component: Bar }
    ]
});

var app = new Vue({
    mixins: [require('spark')],
    router,
    components : {
        Example, Foo, Bar
    },
    data : {

    }
});

and web.php file looks like this:

Route::get('/', 'WelcomeController@show');

Route::get('/home', 'HomeController@show');

// vue-router will handle any other requests
Route::get('/{vue_capture?}', function () {
    return view('welcome');
})->where('vue_capture', '[\/\w\.-]*')->middleware('auth');

and api.php routes file like this:

Route::group([
    'middleware' => 'auth:api'
], function () {

    Route::get('/tasks', function(){
        return \App\Task::all();
    });

});
mniblett's avatar

have you had any luck with this? i'm trying to do the same thing.

does this part of the spark docs help?

Sharing Your API With Your JavaScript Application

Note: This feature requires that you use Vue.js as your application's JavaScript framework.

If your application exposes an API, it can be beneficial to consume that API yourself from your application's front-end JavaScript code. This allows you to share the same API between your application and your API SDKs you may be shipping on various package managers. Spark makes it entirely painless to consume your API in this way. Simply make requests to your API routes using your JavaScript framework as normal. You do not need to pass any token or credentials. All of the authentication will be handled automatically by Spark, which generates "transient", short-lived API tokens behind the scenes automatically when users load your application's pages. These API tokens are automatically refreshed in the background by Spark. So, if you are using Vue, you may simply call your API routes like normal. No additional configuration is necessary to start consuming your API:

this.$http.get('/api/users')
    .then(response => {
        this.users = response.data;
    });
mniblett's avatar

Spark 3.0 is the Vue 2.0 compatibility update

willbrowning's avatar

I have managed to get it working and api calls are also working correctly now.

The issue seemed to be the ->where('vue_capture', '[\/\w\.-]*') in the /{vue_capture?} route.

I've now removed this and moved the function to the HomeController so that all middleware applies to it.

So in web.php I now have:

Route::get('/', 'WelcomeController@show');

Route::get('/home', 'HomeController@show');

// vue-router will handle any other requests
Route::get('/{vue_capture?}', 'HomeController@vue');

and in HomeController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');

        $this->middleware('subscribed');
    }

    public function show()
    {
        $user = \Auth::user();

        return view('home', compact('user'));
    }

    public function vue()
    {
        $user = \Auth::user();

        return view('welcome', compact('user'));
    }

}

It all appears to be working now, so I have spark handling the settings routes then Vue handles any component routes with vue-router so I can build the single page app front-end.

mniblett's avatar

@EmilMoe said: Just for courosity, @mniblett, is it a new license for Spark 1.0, 2.0 and 3.0 ?

Each version so far has been a free upgrade.

1 like
jhull's avatar

Did you have to do something special to make it work with dynamic routes? (Like /foo/:id etc.?

I cant seem to get it to work.

Please or to participate in this conversation.