Laravel Application Development 2021 - 1 - CSC - 006 - Subecz
Laravel Application Development 2021 - 1 - CSC - 006 - Subecz
ISSN 2064-8014
Keywords: Abstract
Web-development Laravel is a useful PHP framework and we can create web-
PHP Framework application easily with this application. It uses the popular MVC
Laravel (model–view–controller) design pattern and based the Symfony
MVC model system. Laravel uses a modular package system, therefore we
Web-application
can expand our application with new modules. It reuses several
Article history: existing components of other frameworks, which helps to create
Received 30 March 2021 a secure operable application quickly. Some of its features and
Revised 20 April 2021 advantages: supports accessing of different databases; presents
Accepted 22 April 2021 utilities which helps in web- application development; simple and
fast routing engine; the web application becomes more scalable;
considerable time is saved in designing. Laravel is open-source
software, licensed by MIT.
1 Introduction
Laravel is the most usable PHP framework for beginner and advanced programmers as well.
It can reduce the time of web- application development and getting to the market with modern object
oriented PHP methods. Its expressive syntax and modern functions are attractive for developers
who want to create robust applications [7]. Using a framework eases the process of development
because it supplies several modules with their connections together. The web-framework gives us a
starting point to the application creation and enables the developer to concentrate on more
interesting issues. Laravel provides such powerful features as expressive database abstract layer
and dependency injection and it’s highly scalable.
In this paper I introduce the main characteristics of this useful and rightly popular framework.
2 Routing
The main function of a web-application framework is to receive the requests from the users
and send responses usually via HTTP(S). It means that the first task during the application process
is to determine the necessary routes. Without the routes the system can’t get in touch with the users.
The route is essentially a URl that makes possible the communication with the outside world with a
known URL-address. Basically a client gives a request for a determined route, which according the
requirements, forwards it to a controller that processes the request. The /routes folder in the project’s
root directory contains the route files.
2.1 Some examples for routing in routes/web.php file
Route::get('route1', function () { return view('welcome'); });
In this example the 'Route' has a method ‘get’ that returns a ‘welcome’ view, which presents a
web page. When a user visits the home page, he is taken to the ‘welcome’ page. This ‘welcome’
view is actually a PHP file: 'welcome.blade.php' in resources/views folder..
Route::post('route2', 'App\Http\Controllers\FileController@read');
211
Zoltán Subecz
In this case the Route’s post method calls the read method of the FileController.
3 MVC architecture
Laravel is based on MVC architecture. The Model-View-Controller (MVC) is a design pattern
that contains three main parts: model, view and controller. These components are so configured to
handle the special development aspects of the application. The View component is intended for user
interface logic. The Controller component receives input data and processes it. It works as interface
between the model and view components. The Model component is the logic that has connection to
the data related to the users. It is the main component of the pattern and represents the transferred
data between the view and controller.
In a typical MVC web-framework (Figure 1.) the controllers handle the user requests and the
business logic, the models represent the data and the views generate the output data [2].
mainpage view:
<body>
<div class="header">
@include("header")
</div>
<div class="content">
@yield("content")
</div>
212
Web-development with Laravel framework
</body>
The above example contains a Blade echo statement {{……. }}. Blade lets you build
hierarchical layouts by allowing the templates to be nested and extended. The @yield directives
act as placeholders for the different sections that a child view can populate and override. The
@section ... @stop directives delimit the blocks of content that are going to be injected into the
master template. Unlike some PHP templating engines, Blade does not restrict you from using plain
PHP code in your templates. In fact, all Blade templates are compiled into plain PHP code and
cached until they are modified, meaning Blade adds essentially zero overhead to your application
5 Controller
In an MVC framework the controller controls the dataflow between the datasets and views.
This is a transport mechanism that takes the users to the presentation layer [2]. The controllers
receive requests, process them and send appropriate responses. The controllers deal with such
tasks as requesting data from database, handling received data from forms and saving data into the
database. The controllers are inside the app/http/controllers directory.
In this example the TaskController has a CallView method with an array definition and passing
this array to a view. Our controller has been created in the app/controllers directory which Laravel
has created for us. There are two types of controllers in Laravel, standard controllers and resource
controllers. Their job is to make sense of the incoming requests and to send an appropriate
response.
213
Zoltán Subecz
We can create a migration file automatically with the next artisan command:
php artisan migrate:make create_users_table
For creating the users table we can use such a migration class:
class CreateUsersTable extends Migration
{
// Run the migrations.
public function up()
{
Schema::create('userss', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('login');
$table->integer('age');
$table->timestamps();
});
}
// Reverse the migrations.
public function down()
{
Schema::dropIfExists('workers');
}
}
As an example, let's create a seeder for the workers table and add a database insert statement
to the run method:
<?php
namespace Database\Seeders;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
class WorkerSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('workers')->insert([
'first_name' => 'John',
'last_name' => 'Hall',
'login' => 'admin',
'age' => '35',
214
Web-development with Laravel framework
]);
}
}
6.3 Query builder
Laravel provides the easy to use query builder that is intended for creating and executing
database queries. The developer can use it for most of the database operations and works perfectly
with Laravel’s all supported database systems. With the query builder we can connect methods into
a chain for bulding complex queries.
215
Zoltán Subecz
Besides the authentication system we can use a simple method for the authorization of the
user operations when the user tries to access a given resource. Even if the user is authenticated,
it’s not sure that he has right to read or write some Eloquent models handled by the application or
database records. Laravel’s authorization services give simple and organized methods to solve such
tasks.
Laravel 8 ships Jetstream that gives a better starting point to new projects. It contains the next
components: two-factor authentication; API support; session management; functions for login and
registration; email verification [5].
9 Middleware
Middleware is used for filtering the HTTP-requests entering the application. The developer
can use middleware for such tasks: authentication and authorization checks; session validation and
session variable modification; reading, setting or modifying the header of the request and response;
transaction logging; API calls, …[5].
10 Validation
Laravel’s Validator class helps with full functionality the validation of e.g. forms and database
models. The validator supports receiving input data; declaring special validation roles and declaring
special validation messages.
216
Web-development with Laravel framework
13 Version history
Laravel version schema uses these conventions: paradigm.major.minor. The major versions
are released evefy year (in September) and the minor versions and patches can be released
frequently. The minor versions can’t include breaking changes, but the major releases can contain
such changes [5]. The next table (Table 1.) contains information about the latest Laravel versions.
Table 1. Laravel version history
Version Release
4 May 2013
5 February, 2015
6 (LTS) September, 2019
7 March, 2020
8 September, 2020
9 (LTS) September, 2021
14 Summary
In this paper I introduced the main characteristics this useful and rightly popular framework,
with covering these topics: routing, MVC architecture, views and templates, controller, interacting
with databases, model and Eloquent, authentication and authorization, middleware, validation, the
service container and request lifecycle, securing the application and testing. I’d recommend Laravel
framework for developers who want to create web-applications in a modern and fast way.
217
Zoltán Subecz
Acknowledgment
This research is supported by EFOP-3.6.1-16-2016-00006 "The development and enhancement
of the research potential at John von Neumann University" project. The Project is supported by
the Hungarian Government and co-financed by the European Social Fund.
References
[1] Martin Bean: Laravel 5 essentials, Published by Packt Publishing Ltd., 2015,
ISBN: 978-1785283017
[2] Hardik Dangar: Learning Laravel 4 Application Development, Published by Packt Publishing Ltd., 2013
ISBN: 978-1783280575
[3] Jesse Griffin: Domain-Driven Laravel, Learn to Implement Domain-Driven Design Using Laravel, 2020, Apress; 1st
ed. edition, ISBN: 1484260228, https://doi.org/10.1007/978-1-4842-6023-4
[4] Arda Kılıçdağı, H. İbrahim YILMAZ: Laravel Design Patterns and Best Practices, Published by Packt Publishing
Ltd., 2014, ISBN: 978-1783287987
[5] Laravel - The PHP Framework For Web Artisans, https://laravel.com/docs/8.x, Accessed 25 Marc. 2021.
[6] Shawn McCool: Laravel Starter, Published by Packt Publishing Ltd., Leanpub, 2012,
ISBN 978-1-78216-090-8
[7] Christopher John Pecoraro: Mastering Laravel, Published by Packt Publishing Ltd., 2015,
ISBN: 978-1785285028
[8] Dayle Rees: Laravel: Code Bright, Publisher: Leanpub, 2013,
ISBN: 978-1471777493
[9] Raphaël Saunier: Getting started with Laravel 4, Published by Packt Publishing Ltd., 2014,
ISBN: 978-1783287031
[10] Sanjib Sinha: Beginning Laravel, Build Websites with Laravel 5.8, Howrah, West Bengal, India, 2017
ISBN: 978-1484225370, https://doi.org/10.1007/978-1-4842-2538-7
[11] Matt Stauffer: Laravel: Up and Running, A Framework for Building Modern PHP Apps,
Publisher: O'Reilly Media, 2016, ISBN-13: 978-1491936085
218