1
votes

I have something strange going on with my Laravel/Vue application and cant figure it out. Within Laravels web.php, I have a simple route

Route::get('api/test', 'UploadController@test');

If I do route:list I can see the correct route

| GET|HEAD  | api/test | App\Http\Controllers\UploadController@test | web,auth  |

I have simplified the controller for now so it is like so

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class UploadController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function test() 
    {
        dd("IN");
    }
}

And the Axios request is simply

<script>
    export default {
        data() {
            return {
                files: {}
            }
        },
        methods: {
            loadFiles() {
                axios.get("api/test").then(({ data }) => (this.files = data));
            }
        },
        created() {
            this.loadFiles();
            Fire.$on('AfterCreate',() => {
                this.loadFiles();
            });
        }
    }
</script>

This is where things get strange. Checking the console, I can see that the request URL is http://localhost:8000/api/test which is correct. I am getting a 200 response, but it is not showing my output, just the whole HTML code for the page.

If I change the request to POST, then I see the output for this function.

Am I missing something here? Why would POST work but GET not?

Thanks

Update Managed to get it working, but not sure why. I have another page that displays users, and the GET request for this was working fine. I then wanted to check my api.php route file to make sure nothing in here was stopping the test. I noticed I still had my user one in here, so I deleted it, and then my user stopped working.

So in api.php, I have added

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::middleware('auth:api')->get('/upload', function (Request $request) {
    return $request->user();
});

And now both work. Why is this?

Thanks

1
try to remove the dd and instead use return response()->json(['abc']);Patrick Schocke

1 Answers

2
votes

You have dd("IN") in your public function test(), which will output HTML (and the CSS/JS) necessary to render the contents of your dd() function in a fancy, interactive element. API calls need to actually return something usable, such as a json response:

public function test(){
  return response()->json(["status" => "OK"], 200);
}

Edit: Also, be aware of conflicting routes. All the routes in routes/api.php are prefixed automatically with /api, so defining the following two routes will be a conflict:

routes/web.php:

Route::get("/api/test", ...);

routes/api.php:

Route::get("/test", ...);

I'm not 100% on which would be used in this situation, but it's likely the one in routes/web.php, as it is defined later in the code. Also, if you use Route::get("/api/test", ...); in routes/api.php, then your actual route would be http://localhost/api/api/test (notice the double prefix), so be aware of that too.