Getting Started with Laravel

By September 27, 2021 Laravel, PHP
Table of Content:

  • Laravel Overview
  • What is MVC ?
  • Laravel fresh Installation
  • Laravel Application Structure
  • Laravel Configuration
  • Basic HTTP request flow with Routing & views

Laravel Overview

  • Laravel was created by Taylor Otwell.
    Since its initial release in June 2011 (version 1)
    the current version of Laravel is 8
  • If you are familiar with Core PHP, Laravel will make your task easier.
  • Laravel is an open-source PHP framework that is robust and easy to understand. It follows a model-view-controller(MVC) design pattern.
  • There are several PHP frameworks available in the market for developing robust web applications like Symfony, Yii, Codeigniter, and many others. But, Laravel has retained the first position in the list of best MVC-based PHP frameworks due to the ease and flexibility it provides to web developers.
  • It saves a lot of time if you are planning to develop a website from scratch. Moreover, a website built in Laravel is secure and prevents several web attacks.

Why laravel ?

  • MVC Support
  • Built-In Authentication and Authorization
  • Packaging System
  • Artisan Console
  • Eloquent ORM
  • Templating engine
  • Task Scheduling
  • Testing

What is MVC ?

The Model View Controller (MVC) design pattern specifies that an application consist of a data model, presentation information, and control information.

MVC architecture comes up with built-in functionalities that developers can use at their best while building your web app. Apart from this, MVC architecture provides better documentation, improved performance, and multiple built-in functionalities compared to other PHP frameworks.

architecture of laravel mvc

Composer:

Composer is a tool that includes all the dependencies and libraries. It allows a user to create a project with respect to the mentioned framework (for example, those used in Laravel installation). Third-party libraries can be installed easily with help of composer.

Download composer: https://getcomposer.org/download/

Check Composer Installed or not: composer -v

Create a new directory anywhere in your system for your new Laravel project. After that, move to the path where you have created the new directory and type the following command there to install Laravel.
composer create-project –prefer-dist laravel/laravel <projectName> “7.0”

terminal

Laravel Application Structure

Here is a brief breakdown of the directories in a common Laravel application:
app/ : It is the application folder and includes the entire source code of the project. It contains events, exceptions, and middleware declarations.
bootstrap/ : Holds the application’s startup script and a few class-map files
config/ : Holds the app’s configuration files. The config folder includes various configurations and associated parameters required for the smooth functioning of a Laravel application.These are usually not modified directly but instead, rely on the values set up in the .env (environment) file at the root of the app
database/ : Houses the database files including migrations, seeds and test factories
public/ : Publicly accessible folder holding compiled assets and of course an index.php file
resources/ : Contains front-end assets such as javascript files, language files, CSS/SASS files and all templates used in the application (called blade templates)
routes/ : All routes in the application are inside here. There are a few different “scopes” of routes but the one we will be focusing on is the web.php file
storage/ : All temporary cache files used by the application, session files, compiled view scripts and log files
tests/ : Contains test files for the application such as unit test

Laravel Configuration

Environment Configuration(.env):

  • While working with basic configuration files of Laravel, the following points are to be noted
  • The .env file should not be committed to the application source control, since each developer or user has some predefined environment configuration for the web application.
  • For backup options, the development team should include the .env.example file, which should contain the default configuration.

Laravel Commands:

Create Controller :
php artisan make:controller EmployerControllers
Create Model :
php artisan make:model Employer
Create Migration :
php artisan make:migration create_employers_table
php artisan make:migration add_salary_to_employers_table –table=employers
Create Middleware :
php artisan make:middleware AgeMiddleware

Basic HTTP request flow with Routing & views

Basic Routes:

Route::get(‘/’, function () {
return view(‘welcome’);
});
Route::get(‘name/{phone}’,function($id) {
echo ‘Phone No: ‘.$phone;
});
Route::get(‘/employer’, ‘EmployerController@upload’)->name(’employer’);

Leave a Reply