This blog post is Human-Centered Content: Written by humans for humans.
Setting Up the Environment
Let’s build a thing! Where to start?
I need an app that has my working composer project installed. That way I can test the functionality as I build it.
There’s several ways to build a local development environment. I personally use Laravel Herd, but this certainly isn’t the only way. For simplicity, let’s follow along with the current Laravel documentation for starting a new app.
First, you’ll need to make sure PHP, composer, the Laravel installer and Node are installed. That sounds like a lot. Luckily, Laravel has made it pretty easy to get all those things!
PHP and composer can be retrieved with the following command depending on your computer’s OS:
Mac:
/bin/bash -c "$(curl -fsSL https://php.new/install/mac)"
Windows:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://php.new/install/windows'))
Already halfway there! Next, let’s grab the Laravel installer. This just adds a simple CLI for creating new Laravel apps (command is OS-agnostic):
composer global require laravel/installer
And lastly, you can get Node here.
We have all the dependencies we need! Now let’s create our app:
laravel new thoughtspotrest
I named mine “thoughtspotrest” because that’s what my package will be named, but feel free to name it whatever you’d like!
The new command will ask several questions. Here’s a quick overview and what my choices were:
- “Would you like to install a starter kit?”
- Laravel provides two starter kit options that come with heaps of functionality pre-installed. Breeze comes with authentication features and view components for a “profile” page, login form and more. It can also pull in frontend frameworks like Livewire or, if you’re into JS, you can get React or VueJS via Inertia. The other starter kit option is Jetstream, which is a more robust version of Breeze. It includes the same authentication features plus session management, API support, and more. If you’d like to learn more about the starter kits, check out the doc here.I’m only building and testing a composer package, not a full-fledged app, so I’ve chosen, “No starter kit.”
- “Which testing framework do you prefer?”
- The options here are Pest and PHPUnit. I’ve worked with both frameworks and had a great time with each. Pest has a neat feature called “Architecture Testing” which goes beyond functionality and looks at architectural rules. This can help keep code clean, making it more maintainable and easier to read. Pest tends to be very easy to read and write once you get the hang of it, so that’s what I’ll choose when we get to testing our composer package.We will be adding tests for our package later, but this option is for the app the package is sitting in. So, the choice here isn’t relevant. I went with “Pest” because that was first in the list.
- “Would you like to initialize a Git repository?”
- Git is an open-source version control system for projects. It’s a way to easily collaborate with a larger team and track changes made to a project repository. If something goes wrong, reverting a change is a cinch. The largest git platform out there is GitHub. It allows you to make your project publicly accessible as well as adding in workflows for testing every change made to the repository.We will be initializing a GitHub repository for our composer package, but this is for the outer app, so we can say “no” here.
- “Which database will your application use?”
- Doing a writeup of each database type and their differences would be what we call in the business “scope creep” for this blog. To be fair, detailing each of the questions so far may be a bit of scope creep, but I digress. If you have no idea where to start, SQLite is a great and lightweight choice for most projects. It’s file-based and doesn’t require setting up another application. Our composer package doesn’t require a database anyways, so I’ll be choosing SQLite to keep things simple.
- “Would you like to run the default database migrations?”
- Database migrations are how you define the schema for your database right inside your code. It makes it much simpler to have every deployment of your app have a matching schema. You can learn more about them here. Again, our package isn’t using the database so the choice here isn’t relevant. I chose “Yes” because it was the first option in the list.
And we have a scaffolded Laravel project! Next, we can navigate into the directory, install the Node dependencies and spin the app up:
cd thoughtspotrest npm install && npm run build composer run dev
Now, you can open the app up in your web browser at http://localhost:8000!