Ligonsker's avatar

Using Laravel as The Full Stack Framework, but only use it with API logic - How bad is this idea?

At first I wanted to build an SPA using Laravel and Vue. But now that I am short on time, I probably need to start using only Laravel meanwhile.

So what if instead of using the web routes, I will only use the api routes from Laravel itself (will only call the Controllers from the API routes), and I'll just treat the frontend as if it's separate and call the /api/* routes with AJAX (jQuery maybe, or fetch api).

This way, if building the app with API in mind, it could be easier to redesign the front end in case I want to replace it?

Are there pros to this idea? And of course, what are the cons? :)

Thanks!

0 likes
13 replies
Snapey's avatar

You still need to serve your frontend from somewhere.

If you want to develop something quickly, do it all as server side rendered blade files.

2 likes
martinbean's avatar

So what if instead of using the web routes, I will only use the api routes from Laravel itself (will only call the Controllers from the API routes), and I'll just treat the frontend as if it's separate and call the /api/* routes with AJAX (jQuery maybe, or fetch api).

This way, if building the app with API in mind, it could be easier to redesign the front end in case I want to replace it?

@ligonsker I don’t really follow? Is that not pretty much a SPA if you just have some random JavaScript calling data from API routes?

If you’re short on time, just build the app “normally” as @Snapey says. It sounds like you’re just trying to solve problems of your own invention.

2 likes
Ligonsker's avatar

@snapey @martinbean I might have confused you with my bad description - yes I will be using Laravel Blade + JS, but whenever I need to do AJAX calls on the front end, I will do it using the api routes file and not the web routes file,

(not talking about the regular routes like myapp/some-route/, which will be in the web file)

Because right now, I place even AJAX calls in the web routes. But what if instead of that, I will "prepare" it and use any AJAX call as an API endpoint? And just treat it as if it's separate, so beside the Blade files that will be rendered by the server, all the dynamic JavaScript content will be treated as API and even use the sanctum middleware:

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

@martinbean I don't think this is an SPA where you have one index.html and you update its content because I still use it with multiple pages

Snapey's avatar

@Ligonsker doing it as api routes saves you nothing and makes authentication the request harder

2 likes
Ligonsker's avatar

@Snapey And what if I decide to switch to Vue or React? Won't all the API routes be ready with all the logic and it will be smoother to do the transition?

Yes the authentication will be harder, but then again it will be ready for an SPA authentication flow

jlrdw's avatar

@Ligonsker use an API when needed otherwise make it a web app. For example the sample video on pulling in weather data, that's an API.

An API should return json or xml, sometimes csv. The developer who uses your api will need instructions for usage in php, java, .net core, etc.

Otherwise what are you trying to do?

Maybe you want to look up PWA's also (progressive web apps).

2 likes
Ligonsker's avatar

@jlrdw I understand, but if I build a web app with Laravel, then it will be the backend of my app as well, and if I decide to maybe make a mobile app that doesn't use the same web views (maybe React Native?), then Laravel will need to serve as an API. So in this way you just separate Laravel to backend and frontend in any way, even if at first you just build a web app.

If I will use Vue/React, then in order to update content, I will need to call my backend somehow, won't that be using the API routes? And I am not talking about using Inertia.js or Laravel UI, but separate "vanilla" Vue/React app

jlrdw's avatar

@Ligonsker there is a difference in an API and a mobile app. A mobile app is written in as you said maybe React Native. If the API is only to provide the data to the mobile app you write it that way to start with.

But a general API is for all the general languages. Generally the developer decides how to take the data and display it.

Use the tools you need per application. I suggest the laravel from scratch free video training.

2 likes
Ligonsker's avatar

@jlrdw ok that makes sense. But what about the case when it's the same app but different frontend? Not talking about completely different app that needs data from your backend

I was just thinking that if for example you have a button on your web app that is used to fetch data and update the UI based on that data, then the same data would be needed on the mobile app version of that button. Won't that be better even for the developer of the other app? (Of course there would be extra API calls from this app since all the initial data that is embedded in the blade files won't be present on other apps)

Because the docs for using Laravel as the API backend, mention that I should use the API routes to get data

martinbean's avatar

@Ligonsker Considering you say you’re short on time, you’re spending an awful lot of time debating building your app that would take longer than if you just built it with Blade views and a handful of controllers than returned JSON if you really needed any AJAX interactivity.

Stop thinking about the future and what you might do, and instead build for now. Otherwise you’ll never write a single line of code if you’re second-guessing what “might” happen in the future.

3 likes
Ligonsker's avatar

@jlrdw yes but this is specifically for Eloquent, I was talking about this: https://laravel.com/docs/9.x/sanctum#spa-authentication

@martinbean Haha yes I know but I just remember that I was working on an app where it had a lot of JS interactivity and it looked so bad - the web routes file was full of AJAX routes and it didn't seem right. Then they wanted to add a new UI AND API and they just needed to add API routes that basically did the same as the AJAX calls on the web app because they did not design it well and it all got mixed and was really badly looking. I just thought that it could have been much better if they made any AJAX request as API instead

martinbean's avatar

@Ligonsker You can create API controllers, then use this middleware from Passport to authenticate requests from JavaScript.

If you use Axios to make AJAX requests, then a cookie will be attached that Passport can parse and use to authenticate the request (without having to use API tokens or anything).

1 like

Please or to participate in this conversation.