DEV Community

Cover image for Cloud-native Laravel
Oscar Nevárez
Oscar Nevárez

Posted on • Updated on

Cloud-native Laravel

What is Laraboot?

Laraboot is a CLI tool that allows Laravel developers to bootstrap and configure a Laravel application using cloud-native technologies which make your developer experience a lot more enjoyable.

Getting started

laraboot cli is distributed as a NPM project so make sure you have NodeJs installed then run:

npm i -g @laraboot-io/cli
Enter fullscreen mode Exit fullscreen mode

Dependencies

We're also going to need Docker installed.

Create a new project

Similar to the Laravel installer you can run laraboot new to initialize a Laravel application.

laraboot new app --php-version=8.0 && cd app
Enter fullscreen mode Exit fullscreen mode

Configure using core tasks

Laraboot has this task concept which resemble Heroku's buildpacks, the tasks participate during the build time allowing te construction of a Laravel application using a low-code approach.

Core tasks allow Laraboot to configure your Laravel application without writing any code.

Tip: you can run laraboot task search * --core-only to discover all the available core tasks.

We are going to use two of those tasks in this occasion model and config these tasks will help us to configure presicely those aspects of the framework.

# this first one is the foundation for other tasks.
laraboot task add @core/laravel-foundation-provider --format=file

laraboot task add @core/laravel-model --format=file
laraboot task add @core/laravel-config --format=file

Enter fullscreen mode Exit fullscreen mode

Data model

Let's create some models, the most common of the examples I've seen in the Laravel forums involve a Post and a Comment.

laraboot model add Post
√ Would you like to define your model now? (y/N) · true
√ Field name? · content
√ Field type? · varchar
√ Is this field unique? (y/N) · false
√ Is this field primary key? (y/N) · false
√ Would you like to add a new field? (y/N) · false
Model **Post** was added

laraboot model add Comment
√ Would you like to define your model now? (y/N) · true
√ Field name? · content
√ Field type? · varchar
√ Is this field unique? (y/N) · false
√ Is this field primary key? (y/N) · false
√ Would you like to add a new field? (y/N) · false
Model **Comment** was added
Enter fullscreen mode Exit fullscreen mode

This configuration is preserved in file laraboot.json and it will be read during the build process.

Configure our application

Something every Laravel developer is familiar with is the configuration of a new application. Laravel provides a simple file configuration system. Over time however, you may find yourself repeating the same exact configuration over and over again.

With Laraboot you only have to do it once, the configuration presset is stored in file and can be reused in another project.

Let's see how:

# Change the application name 
#  with a list of environment variables to look up for 
#  before using default value "App"

laraboot config set config.app/name App -e APP_NAME -e APPNAME
Enter fullscreen mode Exit fullscreen mode

During the build process Laraboot will use @core/laravel-model task to automatically map every configuration set via CLI to Laravel config files.

In this case the file modified will be config/app.php.

Build

laraboot build
Enter fullscreen mode Exit fullscreen mode

We’re ready to build the application. In this step, Laraboot will use the source code and will transform it using build tasks to finally create an OCI image. Please mind the process might take a while especially if this is the first time you run the build command.

If everything goes on without any problem you should have your Laravel project initialized with additional code generated especially for your project in the way you configure it.

What did just happened?

  • Under the hook Laraboot use laravel installer to initialize a new project
  • Using the configured tasks it configured your Laravel application
  • Each task used during the build process contribute to the final OCI image

Run

laraboot run --port=900
Enter fullscreen mode Exit fullscreen mode

Boom! we have our application running. Head to http://localhost:9000 and you’ll see a familiar welcome page.

At this time you might figured it out already. Our OCI image is ready to be used, not to mention deployed. Images are named and tagged using your application name, in this case demo. Check it out

docker images
Enter fullscreen mode Exit fullscreen mode

Conclusion

We used laraboot to create and configure a Laravel application using cloud-first principles. It tooks us some minutes to go from scratch to a custom state from which we can continue the development process as regular. Laraboot tasks are extremely useful to bootstrap a Laravel application, this time we only used two of them.

Discussion (0)