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

MattB's avatar
Level 2

How to use vue router instead of laravel routing just for certain urls

I don't know if this is possible as my knowledge of Vue isn't that strong just yet. Is there a way to have the front (customer-facing) part of a website coded in Laravel with Laravel routing but have the back coded in Vue with Vue routing? I have Vue there now with Vue router, but when you click one of the links it updates the URL as expected and goes to the right page, but when you hit refresh, Laravel (I think) tries to look for the URL but then 404s.

How do I tell that specific part of the site to let Vue router deal with the routing and not Laravel?

0 likes
13 replies
Sinnbeck's avatar

I am doing the somewhat same with react and it works great. This is my route specification. All calls to /admin are passed to a single method that just passes the data to react.

Route::get('/admin/{path?}', 'AdminController@index')
    ->where('path', '.*')
    ->middleware('auth', 'ip')
    ->name('admin');
MattB's avatar
Level 2

Hmm that doesn't seem to be working quite right for me. I can get to the admin dashboard main page which is dashboard.test/admin just fine but when I click one of the links in there it changes the URL to dashboard.test/users (for example) and when you hit refresh it 404s again

Sinnbeck's avatar

Try pointing at the link and see what it says in the lower left corner. is it the proper url? Also remember to not just copy my middleware. I dont assume you have an 'ip' middleware :)

MattB's avatar
Level 2

Yeah the address shows fine in the bottom left, and I'm using my own middleware

MaverickChan's avatar

you are looking for Vue SPA system? and you describe wrong : laravel handles the backend , vue is a frontend framework check vue-router and , in your laravel web routes

Route::any('/{any}', function () {
    return view('welcome');
})->where(['any' => '.*']);

now , if you set vue-router right , it will work.

Sinnbeck's avatar

@maverickchan I believe that is the same as my example except it just returns a view instead of going through a controller which then return a view :)

MattB's avatar
Level 2

I don't think I did describe it wrong. What I mean is I want Laravel routing to handle just the front end customer-facing pages and I wanted Vue router to just handle the back admin page that the users see. That solution you posted just seems to make Vue router handle all the routing by the looks

Sinnbeck's avatar

@mattb Can you post your route + controller code? And the url it points to as well.

MaverickChan's avatar

@sinnbeck if you are using Vue SPA, there is no need to return another laravel view . Vue-router could handle mutilple vue view pages. laravel can provide data as json format , so vue can CRUD them through axios.

in his case , if he is using history mode , laravel could conflict to vue routers.

MaverickChan's avatar

@mattb then you need to divide your system into 2.

if you don't want to do it , then slightly change my code , allow some controller get through.

or , you can do let all normal visit pass through api routes and vue only. the admin part through the web routes. or do the opposite.

Sinnbeck's avatar

@maverickchan Yes exactly. My AdminController::index just returns view('admin') :) I just dont use closures in web.php as that cannot be cached by laravel.

MattB's avatar
Level 2

I found the issue. In my URLs I was appending /pagehere at the end of dashboard.test/. Fixed by appending /admin/pagehere instead. Is this the 'correct' solution or is there supposed to be a more elegant way?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I personally create my routes in react this way (execpt I dont use the domain). I have a function that creates them, that return something like this (a bit more elegant but I hope you get the idea)

return '/admin/' + page;

Please or to participate in this conversation.