0% found this document useful (0 votes)
26 views15 pages

Unit 5 Laravel PHP and MySQL

This document provides an overview of the Laravel framework, including its installation, features, and application structure. It highlights key advantages such as scalability, time efficiency, and security, along with essential components like Composer and Artisan. The document also details the directory structure of a Laravel application and explains routing mechanisms, including basic routing and parameter handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

Unit 5 Laravel PHP and MySQL

This document provides an overview of the Laravel framework, including its installation, features, and application structure. It highlights key advantages such as scalability, time efficiency, and security, along with essential components like Composer and Artisan. The document also details the directory structure of a Laravel application and explains routing mechanisms, including basic routing and parameter handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

College:Vedanth BCA and Bcom College,Vijayapura

Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Unit 5
Introduction to Laravel framework: composer, Artisan, Features of Laravel, Laravel, Installation With
composer, Laravel Application Structure/Directory Structure, Laravel Routing.

Introduction to Laraval Framework


 Laravel is an open-source PHP framework, which is robust and easy to understand. It follows a model-
view-controller design pattern. Laravel reuses the existing components of different frameworks which
helps in creating a web application. The web application thus designed is more structured and pragmatic.
 Laravel offers a rich set of functionalities which incorporates the basic features of PHP frameworks like
CodeIgniter, Yii and other programming languages like Ruby on Rails. Laravel has a very rich set of
features which will boost the speed of web development.
 If you are familiar with Core PHP and Advanced PHP, Laravel will make your task easier. It saves a lot
time if you are planning to develop a website from scratch. Moreover, a website built in Laravel is
secure and prevents several web attacks.

Advantages of Laravel
Laravel offers you the following advantages, when you are designing a web application based on it –
 The web application becomes more scalable, owing to the Laravel framework.
 Considerable time is saved in designing the web application, since Laravel reuses the components from
other framework in developing web application.
 It includes namespaces and interfaces, thus helps to organize and manage resources.

Composer
Composer is a tool which 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.
All the dependencies are noted in composer.json file which is placed in the source folder.

Artisan
Command line interface used in Laravel is called Artisan. It includes a set of commands which assists in
building a web application. These commands are incorporated from Symphony framework, resulting in add-on
features in Laravel 5.1 (latest version of Laravel).

Laravel – Features
Laravel offers the following key features which makes it an ideal choice for designing web applications –
 Modularity
 Testability
 Routing
 Configuration management
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

 Query builder and ORM (Object Relational Mapper)


 Schema builder, migrations, and seeding
 Template engine
 E-mailing
 Authentication
 Redis
 Queues
 Event and command bus

1.Modularity
Laravel provides 20 built in libraries and modules which helps in enhancement of the application. Every module
is integrated with Composer dependency manager which eases updates.

2.Testability
Laravel includes features and helpers which helps in testing through various test cases. This feature helps in
maintaining the code as per the requirements.
3.Routing
Laravel provides a flexible approach to the user to define routes in the web application. Routing helps to scale
the application in a better way and increases its performance.

4.Configuration Management
A web application designed in Laravel will be running on different environments, which means that there will
be a constant change in its configuration. Laravel provides a consistent approach to handle the configuration in
an efficient way.

5.Query Builder and ORM


Laravel incorporates a query builder which helps in querying databases using various simple chain methods. It
provides ORM (Object Relational Mapper) and Active Record implementation called Eloquent.

6.Schema Builder
Schema Builder maintains the database definitions and schema in PHP code. It also maintains a track of
changes with respect to database migrations.

7.Template Engine
Laravel uses the Blade Template engine, a lightweight template language used to design hierarchical blocks and
layouts with predefined blocks that include dynamic content.

8.E-mail
Laravel includes a mail class which helps in sending mail with rich content and attachments from the web
application.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

9.Authentication
User authentication is a common feature in web applications. Laravel eases designing authentication as it
includes features such as register, forgot password and send password reminders.

10.Redis
Laravel uses Redis to connect to an existing session and general-purpose cache. Redis interacts with session
directly.

11.Queues
Laravel includes queue services like emailing large number of users or a specified Cron job. These queues help
in completing tasks in an easier manner without waiting for the previous task to be completed.

12.Event and Command Bus


Laravel 5.1 includes Command Bus which helps in executing commands and dispatch events in a simple way.
The commands in Laravel act as per the application’s lifecycle.

Laravel - Installation
For managing dependencies, Laravel uses composer. Make sure you have a Composer installed on your system before you
install Laravel. In this chapter, you will see the installation process of Laravel.

You will have to follow the steps given below for installing Laravel onto your system –
Step 1: Visit the following URL and download composer to install it on your system. https://getcomposer.org/download/

Step 2: After the Composer is installed, check the installation by typing the Composer command in the command prompt
as shown in the following screenshot.

Step 3: Create a new directory anywhere in your system for your new Laravel project. After that, move to path where you
have created the new directory and type the following command there to install Laravel.
composer create-project laravel/laravel –prefer-dist
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Now, we will focus on installation of version 5.7. In Laravel version 5.7, you can install the complete framework by
typing the following command –
composer create-project laravel/laravel test dev-develop

The output of the command is as shown below –

The Laravel framework can be directly installed with develop branch which includes the latest framework

Step 4: The above command will install Laravel in the current directory. Start the Laravel service by executing the
following command.
php artisan serve

Step 5: After executing the above command, you will see a screen as shown below:

Step 6: Copy the URL underlined in gray in the above screenshot and open that URL in the browser. If you see the
following screen, it implies Laravel has been installed successfully.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Laravel - Application Structure and Directory Structure


Laravel - Application Structure
 The application structure in Laravel is basically the structure of folders, sub-folders and files included in a
project. Once we create a project in Laravel, we get an overview of the application structure as shown in the
image here.
 The snapshot shown here refers to the root folder of Laravel namely laravel-project. It includes various sub-
folders and files. The analysis of folders and files, along with their functional aspects is given below –

App
It is the application folder and includes the entire source code of the project. It contains events, exceptions and
middleware declaration.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Bootstrap
This folder encloses all the application bootstrap scripts. It contains a sub-folder namely cache, which includes
all the files associated for caching a web application. You can also find the file app.php, which initializes the
scripts necessary for bootstrap.
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to
boost your career.

Config
The config folder includes various configurations and associated parameters required for the smooth
functioning of a Laravel application. Various files included within the config folder are as shown in the image
here. The filenames work as per the functionality associated with them.

Database
As the name suggests, this directory includes various parameters for database functionalities. It includes three
sub-directories as given below –
Seeds − This contains the classes used for unit testing database.
Migrations − This folder helps in queries for migrating the database used in the web application.
Factories − This folder is used to generate large number of data records.

Public
It is the root folder which helps in initializing the Laravel application. It includes the following files and folders
−.
htaccess − This file gives the server configuration.
javascript and css − These files are considered as assets.
index.php − This file is required for the initialization of a web application.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Resources
Resources directory contains the files which enhances your web application. The sub folders included in this
directory and their purpose is explained below –
assets − The assets folder include files such as LESS and SCSS, that are required for styling the web
application.
lang − This folder includes configuration for localization or internalization.
views − Views are the HTML files or templates which interact with end users and play a primary role in MVC
architecture.
Observe that the resources directory will be flattened instead of having an assets folder. The pictorial
representation of same is shown below –

Storage
This is the folder that stores all the logs and necessary files which are needed frequently when a Laravel project
is running. The sub-folders included in this directory and their purpose is given below –
app − This folder contains the files that are called in succession.
framework − It contains sessions, cache and views which are called frequently.
Logs − All exceptions and error logs are tracked in this sub folder.

Tests
All the unit test cases are included in this directory. The naming convention for naming test case classes is
camel_case and follows the convention as per the functionality of the class.

Vendor
Laravel is completely based on Composer dependencies, for example to install Laravel setup or to include third
party libraries, etc. The Vendor folder includes all the composer dependencies.
In addition to the above mentioned files, Laravel also includes some other files which play a primary role in
various functionalities such as GitHub configuration, packages and third party libraries. The files included in
the application structure are shown below –
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Laravel - Directory Structure

Console
Console includes the artisan commands necessary for Laravel. It includes a directory named Commands, where
all the commands are declared with the appropriate signature. The file Kernal.php calls the commands declared
in Inspire.php.

If we need to call a specific command in Laravel, then we should make appropriate changes in this directory.

Events
This folder includes all the events for the project.

Events are used to trigger activities, raise errors or necessary validations and provide greater flexibility. Laravel
keeps all the events under one directory. The default file included is event.php where all the basic events are
declared.

Exceptions
This folder contains all the methods needed to handle exceptions. It also contains the file handle.php that
handles all the exceptions.

Http
The Http folder has sub-folders for controllers, middleware and application requests. As Laravel follows the
MVC design pattern, this folder includes model, controllers and views defined for the specific directories.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

The Middleware sub-folder includes middleware mechanism, comprising the filter mechanism and
communication between response and request.
The Requests sub-folder includes all the requests of the application.

Jobs
The Jobs directory maintains the activities queued for Laravel application. The base class is shared among all
the Jobs and provides a central location to place them under one roof.

Listeners
Listeners are event-dependent and they include methods which are used to handle events and exceptions. For
example, the login event declared includes a Login Listener event.

Policies
Policies are the PHP classes which includes the authorization logic. Laravel includes a feature to create all
authorization logic within policy classes inside this sub folder.

Providers
This folder includes all the service providers required to register events for core servers and to configure a
Laravel application.

Laravel – Routing
In Laravel, all requests are mapped with the help of routes. Basic routing routes the request to the associated controllers.
This chapter discusses routing in Laravel.
Routing in Laravel includes the following categories –
1. Basic Routing
2. Route parameters
3. Named Routes

1.Basic Routing
Basic routing is meant to route your request to an appropriate controller. The routes of the application can be defined in
app/Http/routes.php file. Here is the general route syntax for each of the possible request.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Route::get('/', function ()
{
return 'Hello World';
});

Route::post('foo/bar', function ()
{
return 'Hello World';
});
Route::put('foo/bar', function ()
{
//
});
Route::delete('foo/bar', function ()
{
//
});
Let us now understand how to see the Laravel homepage with the help of routing.

Example
app/Http/routes.php

Route::get ('/', function ()


{
return view('welcome');
});

resources/view/welcome.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>

<link href="https://fonts.googleapis.com/css?family=Lato:100"
rel="stylesheet" type="text/css">

<style>
html, body {
height: 100%;
}

body {
margin: 0;
padding: 0;
width: 100%;
display: table;
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

font-weight: 100;
font-family: 'Lato';
}

.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}

.content {
text-align: center;
display: inline-block;
}

.title {
font-size: 96px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Laravel 5</div>
</div>
</div>
</body>
</html>

The routing mechanism is depicted in the following image:

Let us now understand the steps in detail:


Step 1: First, we need to execute the root URL of the application.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Step 2: The executed URL will match with the appropriate method in the route.php file. In our case, it will match to get
the method and the root (‘/’) URL. This will execute the related function.
Step 3: The function calls the template file resources/views/welcome.blade.php. The function later calls the view()
function with argument ‘welcome’ without using the blade.php. It will produce the following HTML output.

2.Routing Parameters
Often in the application, we intend to capture the parameters passed with the URL. To do this, we need to modify the code
in routes.php file accordingly. There are two ways by which we can capture the parameters passed with the URL.
 Required Parameters
 Optional Parameters

Required Parameters
These parameters must be present in the URL. For example, you may intend to capture the ID from the URL to do
something with that ID. Here is the sample coding for routes.php file for that purpose.

Route::get('ID/{id}',function($id)
{
echo 'ID: '.$id;
});

Whatever argument that we pass after the root URL (http://localhost:8000/ID/5), it will be stored in $id and we can use
that parameter for further processing but here we are simply displaying it. We can pass it onto view or controller for
further processing.

Optional Parameters
There are some parameters which may or may not be present in the URL and in such cases we can use the optional
parameters. The presence of these parameters is not necessary in the URL. These parameters are indicated by “?” sign
after the name of the parameters. Here is the sample coding for routes.php file for that purpose.

Route::get('/user/{name?}',function($name = 'Virat')
{
echo "Name: ".$name;
});
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Example
routes.php
<?php
// First Route method – Root URL will match this method
Route::get('/', function () {
return view('welcome');
});
// Second Route method – Root URL with ID will match this method
Route::get('ID/{id}',function($id){
echo 'ID: '.$id;
});

// Third Route method – Root URL with or without name will match this method
Route::get('/user/{name?}',function($name = 'Virat Gandhi'){
echo "Name: ".$name;
});

Step 1: Here, we have defined 3 routes with get methods for different purposes. If we execute the below URL then it will
execute the first method.
http://localhost:8000

Step 2: After successful execution of the URL, you will receive the following output:

Step 3: If we execute the below URL, it will execute the 2nd method and the argument/parameter ID will be passed to the
variable $id.
http://localhost:8000/ID/5
Step 4: After successful execution of the URL, you will receive the following output:
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Step 5: If we execute the below URL, it will execute the 3rd method and the optional argument/parameter name will be
passed to the variable $name. The last argument ‘Virat’ is optional. If you remove it, the default name will be used that we
have passed in the function as ‘Virat Gandhi’
http://localhost:8000/user/Virat

Step 6: After successful execution of the URL, you will receive the following output:

Note: Regular expression can also be used to match the parameters.

3.Named Routes
Named routes allow a convenient way of creating routes. The chaining of routes can be specified using name method onto
the route definition. The following code shows an example for creating named routes with controller –
Route::get('user/profile', 'UserController@showProfile')->name('profile');
The user controller will call for the function showProfile with parameter as profile. The parameters use name method onto
the route definition.
College:Vedanth BCA and Bcom College,Vijayapura
Lecture:Annapoorna Kalloli
Subject:PHP and MySQL(Unit 5)

Assignment Questions
2 Marks Question
1.What is Laravel.
2.List the Advantages of laravel
3.What is Composer in Laravel?
4.What is Artisan?
5.what are the features of Laravel?
6.What are the Application Structure in Laravel?
7.What are the Directory Structure in Laravel?
8.What is Laravel Routing?what are the catagaries of laraval Routing.

5 Marks Question
1.Explain the Features of Laraval.
2.Expalin the Installation of Composer in Laravel.
3.Explain the Laravel Application Structure and directory structure.
4.Explain the Laravel Routing.

You might also like