How to Build a Blog in Laravel - Part 1 - Project Setup

#laravel #php

Today we will create a blogging app using the Laravel framework for PHP. Why Laravel? Laravel has most of the functionality you'll want when creating a web application, pretty much out-of-the-box. With some minor configuration and a little bit of programming we'll be able to build a fully-featured application to host our blog. So, let's get started!

Installing Dependencies and Creating the App

First off, we're going to get our development environment set up. At the bare minimum, we will need a terminal application for running commands and installing dependencies, an IDE or text editor for writing our program, and a web browser for interacting with the app. Once you are ready with those, we will be performing the following steps:

  1. Install Composer
  2. Install Laravel installer
  3. Create a new Laravel project
  4. Install JavaScript dependencies

We install the Laravel framework and its dependencies with the composer command for PHP. If you don't have Composer installed yet, you can get it on the Composer site.

Once Composer is installed, we need to get our hands on the Laravel installer. Run the following command in your terminal:

composer global require laravel/installer

Now that we have the installer, we can create our app folder for the blog:

laravel new blog-app

The command above will prompt you with a few questions. First, it will ask if you want to use any of the official Laravel starter kits. For the purposes of this tutorial, you can say No starter kit.

laravel new blog-app.png

It will also ask which testing framework you will use: Pest or PHPUnit. Choose Pest.

testing framework.png

After installing the core dependencies via Composer, the Laravel installer will ask which database to use. Select SQLite.

database.png

And when it asks if you would like to run the default database migrations, say Yes.

migrations.png

Ultimately, the installer creates a folder called blog-app in the current directory of your terminal, installs all the needed backend dependencies, and creates some database tables.

We need to now go into that folder and run a few more commands to install the JavaScript dependencies of our blog app. Make sure you have Node.js installed before running the commands below.

cd blog-app
npm install && npm run build
composer run dev

Your app should now be available on localhost:8000.

localhost.png

A Good Starting Point

So, what do we have at this point? We have a basic homepage, with all of its dependencies. If you look, you can see exactly which packages are installed:

composer.json

    ...
    "require": {
        "php": "^8.2",
        "laravel/framework": "^11.31",
        "laravel/tinker": "^2.9"
    },
    "require-dev": {
        "fakerphp/faker": "^1.23",
        "laravel/pail": "^1.1",
        "laravel/pint": "^1.13",
        "laravel/sail": "^1.26",
        "mockery/mockery": "^1.6",
        "nunomaduro/collision": "^8.1",
        "pestphp/pest": "^3.7",
        "pestphp/pest-plugin-laravel": "^3.1"
    },
    ...

Dependencies

Dev Dependencies

The dev dependencies are mostly used for testing and/or local development of your app. In a production environment, if you run composer install --no-dev it will skip these dependencies, reducing the overhead of your app.

package.json

    ...
    "devDependencies": {
        "autoprefixer": "^10.4.20",
        "axios": "^1.7.4",
        "laravel-vite-plugin": "^1.0",
        "postcss": "^8.4.47",
        "tailwindcss": "^3.4.13",
        "vite": "^6.0"
    },
    ...

Dev Dependencies

Everything in the JavaScript realm is a dev dependency because Vite bundles our scripts for production.

  • autoprefixer - A PostCSS plugin that automatically adds browser prefixes to your code, for cross-browser compatibility.
  • axios - Makes writing HTTP requests in JavaScript a breeze.
  • laravel-vite-plugin - Provides us a useful plugin for Vite.
  • postcss - A CSS processor that transforms CSS with JavaScript. Used by Vite.
  • tailwindcss - A CSS framework.
  • vite - The preferred JavaScript build tool for Laravel. Built on Rollup.js.

Wrapping up

We might as well keep track of our changes with Git. Run the following inside your blog-app folder:

git init
git add .
git commit -m 'Installed Laravel'

We'll leave it here for now. Next time we'll work on the database schema for our blog, so stay tuned!