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

mahmoudghalayini's avatar

Building API in Web Service

What is the advantages and disadvantages in building an API in web service?

And How can i secure my API while the logged in user doing requests?

0 likes
3 replies
bobbybouwmann's avatar

If you build an API for your website we call this a SPA (Single Page Application). You normally also use a frontend framework to set this up. The advantage here is that you don't need to refresh the whole page, but only parts of it. For example, if you have a table with pagination. If you would go the next page you only need to load the new rows in the table, not the whole page including the sidebar, menu, etc.

The disadvantage is that the development time mostly takes longer and can be more complicated. You have a frontend and a backend and they always need to work together.

Another advantage of an API is that you can change the frontend without changing the backend. Since they both don't depend on each other you can freely change them.

For authentication, you can use Laravel Passport or Laravel Sanctum. They are both created by Laravel and both offer secure ways for your application. It just depends on which use case you have. I would use Passport if you also have an API for the outside world available.

1 like
mahmoudghalayini's avatar

@bobbybouwmann That is so helpful Thank you. My purpose of this question is to know if I built an API in web.php not in api.php is their any problems that might happen? and if i secure it the way i secure a normal application the normal authentication will i face any problem?

bobbybouwmann's avatar
Level 88

You can build all your API routes in web.php. This means that all requests will know the session as well. So they are not really API requests, they just return JSON instead of the HTML page. This approach is easier for authentication.

This also means that your API can only be used inside your own application and not outside of that scope. However, it also requires more work around authentication and so on.

It all depends on your needs and the goal of the project. Both solutions work fine ;)

1 like

Please or to participate in this conversation.