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

oliverbusk's avatar

Web controllers and API controllers

Hi all!

So I've found myself stuck in my thoughts... Consider below resource controller:

DocumentsController.php

public function index(){}

public function show(){}

public function store(){}

public function destroy(){}

Which is used to serve the views for the frontend.

Now this works fine. I can perform the basic CRUD actions for the Documents.

However in my app, I have created a new Vue file called

DocumentViewer.vue, that will be used to show specific documents, that belongs to specific users/other parameters.

I would like to fetch the documents by using Laravel/Vue and a simple AJAX call to the backend. Something like:

data() {
     return {
         documents: []
     }
},
created() {
   axios.get("/documents/userDocuments")
             .then(response => this.documents = response.data);
}

As you can see, I have specified a URL called /documents/userDocuments, which may be referenced in the routes/api.php file like:

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

   Route::get("documents/userDocuments", DocumentsController@getUserDocuments);    

});

This is my initial thoughts. However, for some reason it feels a bit.. "non-Laravel" like, and I am not sure if it goes against best practices.

My concerns are:

  1. I have my normal controller CRUD resource endpoints defined inside my routes/web.php file. Would it be OK to place the endpoint for the Vue endpoints within the routes/api.php file - or should this be placed within the web.php file?

  2. I'm a bit unsure about naming conventions in above. I'm referring to getUserDocuments, which will be a function within my DocumentsController - but this goes a bit against the clean controller structure. But on the other side, creating a new controller doesn't seem right either?

0 likes
6 replies
Nakov's avatar
Nakov
Best Answer
Level 73

My personal opinion is to create a new controller for your API and register the route in your api.php file.

But I will also stick with the cruddy names for the function in the controller instead of having a custom name.

Adam Wathan gave a great talk regarding this, you can watch it here.

1 like
oliverbusk's avatar

@nakov so in my case, it would be another controller called DocumentsController.php, but placed inside for example Api/ folder - so I will have two DocumentsControllers - one for the web and one for the API?

Nakov's avatar

@OLIVERBUSK - Yes, that will be my approach. It is much cleaner and later on you will know where to find things :)

Because naming it differently the next programmer or you in couple of months will have to look outside-in to find the function used for each route.

oliverbusk's avatar

@NAKOV - Thanks! Saw some of the "Cruddy By Design", and it makes sense!

Just one question to my API route, that currently looks like this:

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

   Route::get("documents/userDocuments", "DocumentsController@getUserDocuments");    

});

Would it make sense to set the namespace as well - and change the endpoint:

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

   Route::get("documents", "DocumentsController@index");    

});

Like so?

Nakov's avatar

@OLIVERBUSK - Yes, I like it this way. This will be accessed as /api/documents which makes sense. And yes, the index action is what should return the results. And the namespace is also correct so you won't need to prefix each controller in the routes with Api\DocumentsController.

Please or to participate in this conversation.