Benounnas Oussama's avatar

how vue router works in laravel

i've been learning vue router lately with laravel, following bunch of tutorials (building SPAs), i noticed they add in web.php :

#example 1
Route::any('{any}', function () {
    return view('home');
})->where('any', '.*');
#example2
Route::fallback('HomeController@index');
#example 3
Route::get('{path}', 'HomeController@index')->where('path', '([A-z\d\-/_.]+)?');

to catch all routes wlidcards. my question is how the conversion happens from 'backend routing' to an SPA routing, cause i faced issues with axios, when i do

axios.get("api/clients/1").then(response => {
        console.log(response.data);
      });

inside a ClientInfos.vue component, it gives me back the home view as a response, but when i do

axios.get("../api/clients/1").then(response => {
        console.log(response.data);
      })

i get the proper response i wanted, any explanation to this? thnx

0 likes
2 replies
RamonEgger's avatar
Level 4

Seems like it hits the wrong url when calling

axios.get("api/clients/1").then(response => {
        console.log(response.data);
      });

and then refers to your HomeController.

Try use a leading "/":

axios.get("/api/clients/1").then(response => {
        console.log(response.data);
      })

Test this:

Since you can test the GET route directly in your browser try hitting your target url, for example:

localhost:8000/api/clients/1

If it works axios hits the wrong url.

Please or to participate in this conversation.