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

jeevamugunthan's avatar

Web app and API in same Laravel project?

is it possible.? to make web application and API in same laravel project

0 likes
9 replies
jeevamugunthan's avatar

@zaster thanks for your reply, can you please tell me how can i do this is there any reference

2 likes
zaster's avatar
zaster
Best Answer
Level 9

@jeevamugunthan

routes/web.php

Route::get('/', function () {
    return "Hi from web";
});

http://127.0.0.1:8000/

Outputs

Hi from web

routes/api.php

Route::get('/', function () {
    return "Hi from api";
});

http://127.0.0.1:8000/api/

Outputs

Hi from api

For more information on APIs

https://www.youtube.com/playlist?list=PL8p2I9GklV45xlp9M1NKOqwJxDAXBrCuf https://www.youtube.com/watch?v=MT-GJQIY3EU

Edit :

Web routes use the web middleware

Api routes use the api middleware

https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php#L29-L44

2 likes
siangboon's avatar

yes, both also http requests. Laravel handle web request via routes/web.php where routes/api.php is dedicated for API call and accessble with prefixed with /api/ by default such as http://domainname.com/api/, so you can separate your logic accordingly.. however you can custom it your own...

1 like
Siddhartha's avatar

Hi @jeevamugunthan,

Can you show me sample code that, How you used your own api in web view?

How you pass API data to web application? It's really great help

automica's avatar

@siddhartha routing will determine how external clients access your api.

If you have a method you want to access from your web application, just extract the method that gets your data in api to a service class and then inject that in your controller.

If you have a further question about this, open a new thread and someone can answer it there.

Please or to participate in this conversation.