Introduction To Laravel: Presenter: Mohammad Adil

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 105

INTRODUCTION TO

LARAVEL
PRESENTER: MOHAMMAD ADIL
WHAT IS FRAMEWORK?

▪ A framework is a platform for developing software


applications.
▪ It provides a foundation on which software developers can
build programs for a specific platform.
▪ For example, a framework may include predefined classes
and functions that can be used to process input, manage
hardware devices, and interact with system software.
WHAT IS FRAMEWORK?

▪ Framework provides set of rules and regulations for


developing a software.
▪ Framework provides an architecture to develop software in
a structural and logical manner.
▪ MVC is a most common feature used in frameworks.
INTRODUCTION TO LARAVEL

▪ Laravel is a PHP based web framework for building high-


end web applications using its significant and graceful
syntaxes.
▪ It comes with a robust collection of tools and provides
application architecture.
▪ Moreover, it includes various characteristics of
technologies like ASP.NET MVC, CodeIgniter, Ruby on
Rails, and a lot more.
INTRODUCTION TO LARAVEL

▪ It facilitates developers by saving huge time and helps


reduce thinking and planning to develop the entire website
from scratch.
▪ Open source PHP framework.
▪ Uses the Model-ViewController (MVC) design pattern.
▪ An alternative to CakePHP and CodeIgniter
INTRODUCTION TO LARAVEL

▪ Taylor Otwell developed Laravel in July 2011, and it was


released more than five years after the release of the
Codeigniter.
▪ Laravel is a PHP based web-framework like Codeigniter.
▪ Laravel follows the model-view-controller (MVC)
architectural pattern.
▪ Laravel is one of the most popular PHP frameworks after
Codeigniter
Some essential features provided by
Laravel are:

▪ Routing controllers
▪ Configuration management
▪ Testability
▪ Authentication and authorization of users
▪ Modularity
▪ ORM (Object Relational Mapper) features
▪ Provides template engine
▪ Building schemas
▪ E-mailing facilities
NEED FOR FRAMEWORKS

▪ Framework is a basic templating structure that is used to


develop, test, and deploy applications rapidly and
efficiently.
PURPOSE OF FRAMEWORKS

▪ Provide abstraction
▪ Handles repetitive and menial tasks.
▪ Arranges things in a logical structure.
PURPOSE OF FRAMEWORKS

▪ Reuses and assembles existing components, dependencies,


and libraries
▪ To build Web applications in a structured manner
FEATURES OF LARAVEL

▪ Convention over Configuration


▪ MVC Architecture
▪ Artisan Command for Automation
▪ Helper Functions
▪ Extensive Library
CONVENTION OVER CONFIGURATION

▪ Can predict the dependencies


▪ Automate the configuration related tasks.
▪ Reduces the time and effort required to set up and
configure services
▪ Suitable for Rapid Application Development. (RAD)
MVC ARCHITECTURE
MVC

VIEW CONTROLLER MODEL


ARTISAN COMMAND FOR AUTOMATION

▪ Artisan is the name of the command-line interface included


with Laravel. It provides a number of helpful commands for
your use while developing your application. It is driven by
the powerful Symfony Console component.
▪ Automation tool
▪ Used to create models, views, and controllers
▪ Used to create dummy server
▪ Perform database operations
ARTISAN COMMAND FOR AUTOMATION

▪ Listing All Available Commands


▪ To view a list of all available Artisan commands, you may
use the list command:
▪ php artisan list
HELPER FUNCTION

▪ Global PHP functions


▪ Reduces time and effort
▪ Helper functions are classified as follows:
– Arrays and Objects
– Paths
– Strings
– URLs
– Miscellaneous
EXTENSIVE LIBRARY

▪ Includes a lot of popular and commonly used libraries


▪ Reduces time and effort
▪ Code snippet that allows inclusion of external libraries
using the composer package manager:
▪ Composer require < library-name >
Hiding .env file code:

▪ # Hide a specific file


▪ <Files .env>
▪ Order allow,deny
▪ Deny from all
▪ </Files>
DOWNLOADING AND
INSTALLING LARAVEL
PRESENTER: MOHAMMAD ADIL
DOWNLOADING AND INSTALLING LARAVEL

▪ We need 4 things in this laravel installing..


▪ 1. Wamp Server / Xamp Server / Mamp Server
▪ 2. Composer (Dependency Manager For PHP)
– Composer is a tool for dependency management in PHP.
– Composer provides you CLI to run commands.
– It allows you to declare the libraries your project depends on and it
will manage (install/update) them for you.
– It helps us installing/updating various requirements/components
for our app such as Laravel framework.
DOWNLOADING AND INSTALLING LARAVEL

▪ 3. Text Editor Visual Studio Code, Brackets, Sublime


Text etc.
▪ 4. Git Bash It also provides command line interface to
include third party packages.
Installing Laravel Command Using
Composer

▪ composer global require laravel/installer


Creating New Project Command

▪ laravel new project_name


Change PowerShell to Bash

▪ Step 1: Go to File tab in vs code then go to preferences and then go


to settings.
▪ Step 2: there is a list in left hand side, and from that list select
Features.
▪ Step 3: In features list, select Terimnal then scroll downward and
then click to Edit in settings.json.
▪ Step 4: Remove all data of settings.json then in the curly brackets
pass the path given below.
▪ "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\b
ash.exe"
DIRECTORY STRUCTURE
IN LARAVEL
PRESENTER: MOHAMMAD ADIL
DIRECTORY STRUCTURE IN LARAVEL

▪ app directory: Contains the core code that runs the Laravel
application. Most classes are stored in this directory and its
subdirectories.
▪ bootstrap directory: Includes the code that collates all files
spread throughout the directories and run the application.
▪ config directory: Includes files related to configuration of
different components.
▪ database directory: Contains database migrations, seeds,
model factories, and the SQLite database that will be used in
subsequent sessions.
DIRECTORY STRUCTURE IN LARAVEL

▪ public directory: Includes files that must be exposed to the


Internet. Thus, the index.php file calls the app.php file in the
bootstrap directory to start the application whenever it
receives a request. All HTML, CSS, and Image files are also
stored in this directory.
▪ resources directory: Includes the views that will be used to
display the final Web page as well as the un-compiled assets
related to JavaScript and CSS.
▪ routes directory: Provides the route definition for the
application that will match the request with its response
DIRECTORY STRUCTURE IN LARAVEL

▪ storage directory: Includes data generated by the Laravel


framework for storage and cache.
▪ tests directory: Contains the test specifications for
automated testing.
▪ vendor directory: Contains information on third party
dependencies imported by the composer dependency
manager.
REMOVING PUBLIC
FROM URL
PRESENTER: MOHAMMAD ADIL
ROUTING IN
LARAVEL
PRESENTER: MOHAMMAD ADIL
ROUTING IN LARAVEL
▪ Routing is one of the essential concepts in Laravel.
▪ Routing allows you to map the request with the
appropriate location.
▪ Routing in Laravel allows you to route all your application
requests to its appropriate controller.
ROUTING IN LARAVEL

REQUEST INDEX INDEX


CONTROLLER
FirstProject/Index METHOD VIEW

REQUEST ABOUT ABOUT


CONTROLLER
FirstProject/About METHOD VIEW
ROUTING IN LARAVEL
▪ All the routes in Laravel are defined within the route files
that you can find in the routes sub-directory.
▪ These route files get loaded and generated automatically
by the Laravel framework.
▪ Routes  web.php
AVAILABLE ROUTER METHODS
▪ The router allows you to register routes that respond to any
HTTP verb:
▪ Route::get($uri, $callback);
▪ Route::post($uri, $callback);
▪ Route::put($uri, $callback);
▪ Route::patch($uri, $callback);
▪ Route::delete($uri, $callback);
▪ Route::options($uri, $callback);
CONTROLLERS AND
VIEWS IN LARAVEL
PRESENTER: MOHAMMAD ADIL
CONTROLLERS

▪ In the MVC framework, the letter ‘C’ stands for Controller.


▪ It acts as a directing traffic between Views and Models.
▪ Controllers are php classes.
▪ Controllers are used to create application logic.
▪ Controllers contains one or more than one methods which
can return different types of data.
CONTROLLERS

▪ Separates the controller/application logic from the


presentation logic.
▪ Controllers can be seen at app/Http/Controllers.
▪ Every controller class must extends / inherits Controller
class and this parent controller class is location in
App\Http\Controllers.
CONTROLLERS

▪ Move requests and responses from the routes file to their


own separate files and functions.
▪ Organizes Web applications.
▪ Easy to implement Create, Read, Update, and Delete
(CRUD) operations.
▪ Can be initiated using: Single action controllers and
resource controllers
CONTROLLERS

VIEW CONTROLLER MODEL


ROUTING IN LARAVEL

REQUEST INDEX INDEX


CONTROLLER
FirstProject/Index METHOD VIEW

REQUEST ABOUT ABOUT


CONTROLLER
FirstProject/About METHOD VIEW
COMMAND FOR CREATING CONTROLLER

▪ php artisan make:controller < Controller-Name>


▪ Example:
▪ php artisan make:controller HomeController
VIEWS

▪ Provides User-Interface of the website.


▪ Provides the modularity required for Web application
development.
▪ Located at resources\views.
▪ The default view for Laravel framework
is welcome.blade.php
▪ Separates the controller/application logic from the
presentation logic.
PASSING DATA FROM
CONTROLLER TO VIEW IN
LARAVEL
PRESENTER: MOHAMMAD ADIL
PASSING DATA FROM
CONTROLLER TO VIEW
▪ There are various ways of passing data to views:
▪ By using the name array
▪ By using with() function
▪ By using compact() function
Name array

▪ The name array is the array of data that is passed as the


second parameter to the view() method.
▪ Example
▪ public function Index()
▪     {
▪         return view('Home.Index‘ ,
['std1'=>'Adil','std2'=>'Amir','std3'=>'Osama']);
▪     }
Name array

▪ We can pass multiple arrays from controller to view.


with() function

▪ We can also use the with() function to pass the data to views.


The 'with()' function contains two parameters
▪ Example:
▪ public function Index()
▪     {
▪         $name = "Learning Never Ends";
▪         return view('Home.Index')->with('name',$name);
▪     }
compact() function

▪ The compact() function is also used to pass the data to views. It


contains a single parameter, i.e., the name of the variable.
▪ Example:
▪ public function Index()
▪     {
▪         $name = "Learning Never Ends";
▪         return view('Home.Index',compact('name'));
▪     }
BLADE TEMPLATE
ENGINE IN LARAVEL
PRESENTER: MOHAMMAD ADIL
BLADE TEMPLATE ENGINE

▪ Blade Template engine is used to simplify the process of


creating views.
▪ It makes the syntax writing very simple.
▪ The blade templating engine provides its own structure
such as conditional statements and loops.
▪ View is also called Blade Template.
BLADE TEMPLATE ENGINE

▪ To create a blade template, you just need to create a view


file and save it with a .blade.php extension instead of
.php extension.
▪ The main advantage of using the blade template is that we
can create the master template, which can be extended by
other files.
▪ Blade is a lightweight template language that provides
multiple short codes.
WHY BLADE TEMPLATE?
▪ Blade template is used because of the following reasons:
▪ Displaying data
▪ If you want to print the value of a variable, then you can do
so by simply enclosing the variable within the curly brackets.
▪ Syntax
▪ {{ $variable }}
▪ In blade template, we do not need to write the code
between <?php echo $variable; ?>.
WHY BLADE TEMPLATE?
▪ Blade Template Control Statements
▪ Blade templating engine also provides the control
statements in laravel as well as shortcuts for the
control statements.
▪ @if @else
▪ @if @elseif @else
▪ @switch
WHY BLADE TEMPLATE?
▪ Blade Loops
▪ The blade templating engine provides loops such as
@for, @endfor, @foreach, @endforeach, @while,
and @endwhile directives.
▪ These directives are used to create the php loop
equivalent statements.
WHY BLADE TEMPLATE?
▪ Null Coalesce Operator
▪ In blade template, the syntax of Null Coalesce
operator can be written as:
▪ {{ $variable ?? 'default value'}}

▪ The above syntax is equivalent to <?php isset($variable) ?


$variable : ?default value? ?>
MASTER PAGE LAYOUT OR
TEMPLATE INHERITANCE
IN LARAVEL
PRESENTER: MOHAMMAD ADIL
MASTER PAGE LAYOUT
▪ Master page layout defines the common layout across all
the web pages.
▪ All the web applications have the master page layout to
define the common layout across all the web pages.
▪ The blade templating engine defines the master layout that
can be extended by all the web pages.
▪ The master page layout is available in the
/resources/views/layouts/ directory.
▪ Common name master.blade.php
FORM HANDLING
IN LARAVEL (PART-1)
PRESENTER: MOHAMMAD ADIL
FORM HANDLING IN LARAVEL

▪ It is a Web page containing fields


▪ It is used by users to enter information
▪ It passes entered data from a client to a server
FORM HANDLING IN LARAVEL

▪ Steps for handling HTML forms and process information


are as follows:
– User enters information in an HTML form and sends it to the
Web server.
– Processing the information
– Sending output back to the Web browser
Laravel | CSRF Protection

▪ Cross-Site Request Forgery (CSRF) is a type of attack that


performed by the attacker to send requests to a system
with the help of an authorized user who is trusted by the
system.
▪ Laravel provides protection with the CSRF attacks by
generating a CSRF token. This CSRF token is generated
automatically for each user. This token is nothing but a
random string that is managed by the Laravel application
to verify the user requests.
Laravel | CSRF Protection

▪ How to Use: This CSRF token protection can be applied to


any HTML form in Laravel application by specifying a
hidden form field of CSRF token. The requests are validated
automatically by the CSRF VerifyCsrfToken middleware.
FORM HANDLING
IN LARAVEL (PART-2)
PRESENTER: MOHAMMAD ADIL
FORM HANDLING IN LARAVEL

▪ Follow these 3 steps before creating forms in laravel.


▪ 1st Step:
▪ composer require laravelcollective/html
FORM HANDLING IN LARAVEL

▪ Second Step:
▪ Go to config directory > app.php > providers
▪ Collective\Html\HtmlServiceProvider::class,
FORM HANDLING IN LARAVEL

▪ Third Step:
▪ Go to config directory > app.php > aliases
▪ 'Form' => Collective\Html\FormFacade::class,
▪ 'Html' => Collective\Html\HtmlFacade::class,
FORM HANDLING IN LARAVEL

▪ {{ Form::open(['url'=>'/add', 'method'=>'post']) }}
▪ Form components here….
▪ {{ Form::close() }}
FORM HANDLING IN LARAVEL

▪ Label and textbox


▪ {{ Form::label('Name:') }}
▪ {{ Form::text(‘name') }}
▪ Number
▪ {{ Form::label('salary','Salary:') }}
▪ {{ Form::number('name', 'value') }}
FORM HANDLING IN LARAVEL

▪ Password field
▪ Form::password('password');

▪ File Upload
▪ Form::file('image');
FORM HANDLING IN LARAVEL

▪ Radio and checkbox


▪ Form::checkbox('name', 'value');
▪ Form::radio('name', 'value');
FORM HANDLING IN LARAVEL

▪ DropDownList
▪ Form::select('size', array('L' => 'Large', 'S' => 'Small'));

▪ Submit button
▪ {{ Form::submit('Submit') }}
FORM HANDLING IN LARAVEL

▪ LaravelCollective repository is created by the open source


community to help developers who use Form builder class.
FORM VALIDATIONS
IN LARAVEL
PRESENTER: MOHAMMAD ADIL
FORM VALIDATIONS IN LARAVEL

▪ Required
▪ Email
▪ Min
▪ Max
▪ Numeric
FORM VALIDATIONS IN LARAVEL

▪ Alpha Dash
▪ Alpha
▪ Regex
▪ nullable
FORM VALIDATIONS IN LARAVEL

▪ Size
▪ Boolean
▪ In
▪ Not_in
▪ url
▪ Lt
▪ Gt
FORM VALIDATIONS IN LARAVEL

▪ Lte
▪ Gte
▪ Between
▪ Digits
▪ Same
▪ Starts_with
▪ Ends_with
FORM VALIDATIONS IN LARAVEL

▪ Date
▪ Date_format
WORKING WITH
MODELS IN LARAVEL
PRESENTER: MOHAMMAD ADIL
MVC ARCHITECTURE
MVC

VIEW CONTROLLER MODEL


THE ELOQUENT ORM
IN LARAVEL
PRESENTER: MOHAMMAD ADIL
What is Object Relational Mapping
(ORM)?

▪ ORM or Object Relational Mapping is a system that


implements the responsibility of mapping the Object to
Relational Model.
▪ That means it is responsible to store Object Model data
into Relational Model and further read the data from
Relational Model into Object Model.
What is Object Relational Mapping
(ORM)?
What is Object Relational Mapping
(ORM)?
What is Object Relational Mapping
(ORM)?

LARAVEL
APPLICATION
THE ELOQUENT ORM

▪ One of the most important feature of the laravel framework


is that, it comes with built in ORM (Object Relation Mapping)
called Eloquent ORM.
▪ Eloquent ORM refer to an advanced implementation of the
PHP Active Record Pattern, which makes it very easy to
interact with application database.
▪ Eloquent ORM is the very powerful yet very expressive ORM,
which allow us to work with the database objects and
relationships using much eloquent and expressive syntax.
THE ELOQUENT ORM

▪ In Laravel, each database table is mapped into


corresponding eloquent model and each of the eloquent
model object include various methods for saving,
retrieving and updating the database.
▪ The Eloquent ORM included with Laravel provides a
beautiful and simple for working with your database.
▪ Each database table has a corresponding "Model" which is
used to interact with that table.
SETTING UP DATABASE,
TABLES AND MIGRATIONS
IN LARAVEL
PRESENTER: MOHAMMAD ADIL
SETTING UP DATABASE

▪ STEP: 1
▪ Before getting started, be sure to configure a database
connection in config/database.php.
▪ So you have to made modification in .env file for database
purpose not in config/database.php.
THE ELOQUENT ORM

▪ To get started, create an Eloquent model. Models typically


live in the app directory.
Defining An Eloquent Model

▪ STEP: 2
▪ class User extends Model
▪ {

▪ }
▪ You may also generate Eloquent models using the
make:model command.
Defining An Eloquent Model

▪ STEP: 2
▪ Php artisan make:model Employee

▪ If you want to create migration of model then,


▪ Php artisan make:model Employee –m
▪ All migrations are located in database > migrations
directory.
▪ STEP: 3
▪ Write the columns you want in your table in your migration
file in “up” function.
▪ Use properties in model class like $table, $PrimaryKey,
$timestamps, $fillable.
▪ STEP: 4
▪ Setting up the Default String Length of varchar columns in
your table.
▪ use Illuminate\Support\Facades\Schema;
▪ Copy this namespace from your migration file and paste
into app > Providers > AppServiceProvider.php file.
▪ And then add this line in boot function
▪ Schema::DefaultStringLength(191);
▪ STEP: 5
▪ After writing migration code then run your migrations.
▪ After running the migrations, all tables will be created in
your database automatically.
▪ Command for running the migrations,
▪ Php artisan migrate
CREATING RESOURCE
CONTROLLER IN LARAVEL
PRESENTER: MOHAMMAD ADIL
CREATING RESOURCE CONTROLLER

▪ Create a resource controller, because resource controller


will provide you all the methods in a controller, which you
will be used in your crud application.
▪ Command for creating resource controller.
▪ Php artisan make:controller EmployeeController
▪ Php artisan make:controller EmployeeController -r
RESOURCE CONTROLLER

▪ Create the route.


▪ Example
▪ Route::resource('employee','EmployeeController');
▪ This line will create all the routes for your employee
controller.
RESOURCE CONTROLLER

▪ php artisan route:list


▪ This command will display you all the routes for your crud
application.
FETCHING / RETRIEVING
DATA FROM DATABASE IN
LARAVEL
PRESENTER: MOHAMMAD ADIL
INSERTING DATA IN
DATABASE IN LARAVEL
PRESENTER: MOHAMMAD ADIL
UPDATE DATA IN
DATABASE TABLE IN
LARAVEL
PRESENTER: MOHAMMAD ADIL
DELETE DATA FROM
DATABASE TABLE IN
LARAVEL
PRESENTER: MOHAMMAD ADIL

You might also like