However, there is another – less famous but very powerful – option when it comes to creating SPAs – an option that offers the advantages derived from a double development stack: Inertia.
Why ‘default’ Laravel is not always the best choice for a modern full-stack experience
Everyone knows Laravel’s potential as a web development framework. I love Laravel – it is a very powerful tool, with elegant syntax, every possible back-end feature and with excellent support even for static front-end production through the Blade template engine. However, choosing Laravel as a full-stack development tool in order to create SPAs with modern features is not a solution without its drawbacks. Let’s take a look at the main issues:
– although a front-end library, such as React, can be hooked onto Laravel, there is no real link between the two worlds; they remain two separate tools put together in a single context
– in a Laravel app, every common action – such as clicking on a link – involves a new HTTP request with a consequent reload of the page and loss of the ‘state’, resulting in an old-fashioned user experience
– passing data between back-end and front-end and viceversa requires continuous fetch and Axios asynchronous requests
Nonetheless, like many developers, I love Laravel. Using Laravel on the backend, in the context of web-based applications that don’t imply the kinds of operations that make Java and Python more attractive solutions, such as banking apps or augmented reality concerns, is certainly a powerful solution.
PHP is a great back-end development language for producing web-based applications such as B2Bs and enterprise tools, especially with the features introduced in the recent version 8. Taylor Otwell has done a great job in abstracting powerful Symfony functionalities, and a traditional tool such as Laravel built on top of PHP 8 on the back-end makes the server-side developer experience very rewarding.
So, how is it possible to reconcile the benefits of Laravel and PHP as back-end tools, despite the drawbacks that come with these tools in the production of modern SPAs? Inertia provides the answer, glueing Laravel perfectly to a client-side framework, like Vue or React, overcoming many of the drawbacks previously discussed in a very elegant and satisfying way.
– Laravel’s server-side traditional features, such as authentication, authorization, routing management, validation strategies, CSRF protection, controller and model management, can then be used
– the power of Eloquent ORM is available, but so is the ORM of the users’ choice; there is also the option not to use an ORM at all
– native support for MySQL, MongoDB and PostgreSQL
– the benefits of using Laravel Mix to compile assets can be leveraged
– there’s no need to install extra libraries such as Axios, NextAuth or Iron Session, nor are there CORS issues to handle
– the first visit to the page loads pure HTML, and data is subsequently loaded to the page without a full reload through XHR and JSON
– filename manipulations aren’t required in order to leverage routing. Instead, use the traditional Controller to view component data flow in handling URL requests
– React/Vue/Svelte is linked directly to the Laravel back-end, meaning properties and values are passed from Laravel controller’s computing to JS front-end component and vice versa
– enjoy powerful Breeze and Jetstream packages in official combination with Inertia
Execution time, concurrency, asynchronous requests, async processing enabling faster build times and increasing flexibility are some of the Node upsides to PHP. However, the latter’s eighth version provides many powerful paradigms of its own, and is already a lot faster and much more flexible than in the recent past. While many developers decide to go serverless, splitting their apps into many parts, Inertia has proved that you can build big and powerful React/Vue/Svelte SPA monoliths.
Recommended article: How to Create a Catalog with Stencil