Getting started with Laravel

Lynne
5 min readApr 22, 2024

--

Photo by makeameme in makeameme.org

What is a framework?

A framework simply gives you a set of code (or a frame/base structure) to which you start building on top.

Laravel is a full-stack PHP framework that allows you to build end-to-end applications (client and server-side applications).

Why Laravel?
1. It has one of the best documentations.
2. It has good technical support.
3. It has been in the market for long.

Laravel uses an MVC (Model, View, Controller) architectural pattern.

What is the MVC architecture?

  • Model — how things are written in the database (structure in the database).
  • View — what you see / The view visualizes the response
  • Controller — Used for passing specific configurations needed to give back resources to the user. It works between the database and the client.

This MVC architectural pattern allows better organization, easy implementation, and helps you know at what point your application has failed.

How does Laravel work?

Let’s look at the Lifecycle perspective
Let’s say you request a YouTube video. This request goes to the server. The server is where the API is. The API lies between the database itself and the client. The API must be initialized first before using it. Initializing it is creating an instance of that application.
The framework creates the first instance and runs the whole application.

The Laravel application:

  1. Starts by running public/index.php. This is the entry point. It has an autoloader file, which handles creating the first instance of the Laravel application.
  2. It then proceeds to the kernel — this is the console or http kernel. This kernel will redirect it to where it needs to go in terms of the resource that the client has requested. It handles operations on logging, error handling, detecting the application’s environment, and passing it via middleware. You may ask, what is middleware?
    Middleware acts like a security guard when you attend an event. It controls who can access the database, at what point you access the database, and how long you can access the database.
  3. It then goes to the Service provider, which registers all the services that come with Laravel and boots them. It simply brings in a service and allows your application based on that service.
  4. Finally, it goes to the Routing & controller — You may have many resources within your database. Routing tells us where we need to go, like which resource we need to access. You may also need to perform some CRUD operations, for example, the operation of segregating between entries in the database. This is performed by the controller. It answers the question, how can I serve you what you need? or how can I serve you a particular resource?

Installation of Laravel
Creating your first Laravel project

We are going to create a shop API
You need to make sure you have the following installed:

  • Composer
  • PHP
  • Xampp or Laragon server
  • Postman

First, change the directory to where you’d like to create your project.

Then, use the following command to create the shop-api project:

composer create-project laravel/laravel shop-api

This command creates the arsenal/ tools of trade that you are going to use in your Laravel application. It also installs all the dependencies that are needed.

Running the first application

First, make sure you are inside the directory which you have created.
So, you use the command:

cd shop-api

We then use the command below to run the application:

php artisan serve

The command artisan orchestrates and runs the application.

Let’s explore the directory structure of your first Laravel application.

Laravel app directory structure

From the image above, we can see the shop-api app contains 10 directories. This is assuming you are using Laravel 10. Let’s look at each in detail.

  • app — This is where your application resides. You write all your logic or core code of your app here.
    It contains the following sub-directories:
    - Console — contains all custom Artisan commands for your app.
    - Exceptions — contains all custom exceptions for your app.
    - Http — contains controllers, middleware, and Kernel.php file. This is where you write your code to handle requests entering your app.
    - model contains Eloquent model classes. Models simply allow you to query for data in your tables and insert new records into the tables.
    - providers — contains all the service providers for your app.
  • bootstrap — This directory has the app.php file. What does this app.php file do? It bootstraps the framework.
    It also contains the cache directory which contains framework generated files for performance and optimization
  • config — This is where we write our configurations. It contains all of the app’s configuration files.
  • database — contains database migrations, model factories, and seeds.
  • public — has the index.php file which we talked about earlier as being the entry point of the application. The public folder is also where you can house your assets such as images.
  • resources — This directory contains views and raw uncompiled assets like CSS and JavaScript.
  • routes — As written earlier, the routes answers the question where do we want to go? This routes directory contains all the route definitions for your application.
  • storage — contains logs, compiled Blade templates, file based sessions, file caches and other files that are generated by the framework.
  • tests — This contains your automated tests.
  • vendor — This contains Composer dependencies.

The other files in the Laravel app are:

  • .editorconfig — This is used for defining coding styles by developers.
  • artisan — orchestrates and runs the application
  • .env file is more or less like the environment variables — It contains the configurations for your application.
  • .env.example, which is generated automatically
  • .gitattributes — specifies files or file options in the app directory.
  • .gitignore — ensures that files that aren’t tracked by Git always remain untracked.
  • composer.json — This file is used for specifying the dependencies( libraries and packages with their versions) needed for the app.
  • composer.lock — Ensures package versions or dependencies are consistent when using a common Laravel project among developers by preventing one from automatically obtaining the latest version of the packages.
  • package.json — handles the project’s dependencies. It also contains information about the project like the name and version.
  • phpunit.xml — The environment variables defined in this file are useful when running tests.
  • README.md — You have probably seen this file in many projects, not just Laravel apps. It enables people understand more about your project when they come across it.
  • vite.config.js — Vite helps bundle your app’s JS and CSS files into assets that are ready for production and development.

Depending on your Laravel version, you may check out the directory structure in detail on the Laravel documentation website.

Happy coding in Laravel!

References:

--

--

Lynne

I write about digital health, and web development (HTML, CSS, JS, PHP, Laravel), with Python and Django tutorials on the way. Join me in my journey in tech ❤️