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

JohnRivs's avatar

Using vue-router with Laravel

I can't seem to get this working. I want to use Laravel as an API back-end and let vue-router handle the routing. Can someone share an example of vue-router replacing the Laravel's router role?

Right now, I'm using this sort of hack, where I return the blade file where I load vuejs:

if($e instanceof NotFoundHttpException) {
    return view('index');
}

I also have a middleware that returns the same view if the request is not been made by AJAX.

0 likes
6 replies
cipsas's avatar

@JohnRivs in my routes.php file you can use something like this:

....

Route::group( [ 'prefix' => 'admin' ], function () {
        Route::group(['middleware' => ['adminAuth', ]], function(){
            Route::any( '/{any?}/{any2?}', 'Admin\DashboardController@index' );
    });
});
...

Line Route::any( '/{any?}/{any2?}', 'Admin\DashboardController@index' ); is the key

notanerddev's avatar

Which version of Vue and Vuerouter are you using.. Sorry can't help with your problem. But ive read that Vuerouter only works with Vue 0.12+.

I haven't even started trying it, waiting for a V1.0.0

colourmill's avatar

The most straight forward way is to create a catch all route and point it to a view.

Route::get('/{catchall?}', function () {
    return response()->view('index');
})->where('catchall', '(.*)');

You need this (or manually add all your "vue" routes) because at some point someone might refresh their page and laravel would not know what to do with it. Also make sure your API and other "laravel" routes are loaded first.

6 likes

Please or to participate in this conversation.