0% found this document useful (0 votes)
22 views26 pages

Framework

Uploaded by

verifiednot162
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)
22 views26 pages

Framework

Uploaded by

verifiednot162
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/ 26

Framework

❑A Framework or software framework is a platform for developing software


application.
❑Basically, its simplified our work and reduced the amount of code by providing
some ready made functionality.
LARAVEL
❑LARAVEL is a free open-source PHP based web framework.
❑It has a very rich set of features which will boost the speed of Web
Development.
❑If you familiar with Core PHP and Advanced PHP, Laravel will make your
task easier.
❑It will save a lot time if you are planning to develop a website from scratch.
❑Website built in Laravel is also secure and It prevents the various attacks
that can take place on websites.
❑Laravel follows the model–view–controller (MVC) architectural pattern.
❑Laravel offers a robust set of tools and an application architecture that
incorporates many of the best features of frameworks like CodeIgniter, Yii,
ASP.NET MVC, Ruby on Rails, Sinatra, and others.
❑Taylor Otwell developed Laravel in July 2011, and it was released more
than five years after the release of the Codeigniter framework.
❑Laravel is one of the most popular PHP frameworks after Codeigniter.
Laravel – Features
Open Source
Its free and Large community support

MVC

❑Laravel uses the MVC model, therefore there are three core-parts of the framework
which work together: models, views and controllers.
❑Controllers are the main part where most of the work is done. They connect to
models to get, create or update data and display the results on views, which contain
the actual HTML structure of the application.

Blade Templating Engine

❑Laravel is using Blade templating engine that quite easy to use and more powerful.
❑It also allows the use of plain PHP code in Blade templating engine files.
❑Blade Template Engine file has .blade.php extension.

Artisan
❑Artisan is the command line tool that used to control parts of Laravel. There are a lot
of commands available to create models, controllers and other resources needed for
development.
Inbuilt Authentication

❑Laravel contains an inbuilt authentication system, you only need to configure models,
views, and controllers to make the application work. No any custom code required.

Migration

❑ Laravel Migration is an essential feature in Laravel that allows you to create,


modify and share the application's database schema.
❑ Laravel Migration allows you to add a new column or delete the records in your
database without deleting the records that are already present.

Unique Unit-testing

❑Laravel provides a unique unit-testing. Laravel framework can run several test cases
to check whether the changes harm the web app or not. In Laravel, developers can
also write the test cases in their own code.

Event Handling
The framework is capable of handling events across the application. You can create
event listeners and event handlers that are similar to the ones from NodeJs.
Query Builder and Object-Relational Mapper (ORM)

❑An effective ORM allows the developers to query the database tables by using the
simple PHP syntax without writing any SQL code.
❑Each database table has a corresponding "Model" which is used to interact with that
table.

Intact Security

❑ Laravel has an inbuilt web application security, i.e., it itself takes care of the
security of an application.
❑ It uses "Bcrypt Hashing Algorithm" to generate the salted password means that
the password is saved as an encrypted password in a database, not in the form of
a plain text.

Libraries

❑Laravel is very popular as some Object-oriented libraries, and pre-installed libraries


are added in this framework, these pre-installed libraries are not added in other php
frameworks.
❑One of the most popular libraries is an authentication library that contains some
useful features such as password reset, monitoring active users, Bcrypt hashing, and
CSRF protection.
Routing

Laravel provides a flexible approach to the user to define routes in the web
application. We can define routes in very simple and easiest way.

E-mail

Laravel includes a mail class which helps in sending mail with rich content and
attachments from the web application.

Configuration Management

Easy Configuration. Laravel provides a consistent approach to handle the


configuration in an efficient way.

Composer

❑Composer is a tool for dependency management in PHP.


❑Third party libraries can be installed easily with help of composer.
❑It allows you to declare the libraries your project depends on and it will manage
(install/update) them for you.
Environment Setup Laravel
Steps to follow :

❑ Installation of PHP Local Server (WAMP/XAMP)


❑ Installation of Composer
❑ Installation of Laravel Globally
composer global require laravel/installer

❑Creating a new project


laravel new example-app

❑start Laravel's local development server using the Laravel's


Artisan serve command:
php artisan serve

❑Installation of code editor (VS Code)


Laravel – Directory Structure
app: This directory contains the core code of
the application.
http->Controller – Controller will store here
http->Middleware – Acts as a guard. Define
access allowed or not.
bootstrap: This directory contains the
application bootstrapping script to Speed up
the process
config: This directory contains configuration
files of application.

database: This folder contains your


database migration and seeds.

public: This is the application’s document


root. It starts the Laravel application.

Resources: Contains JavaScript, CSS, Images,


Views etc.
Laravel – Directory Structure

storage: This directory contains App storage,


like file uploads etc. Framework storage
(cache), and application-generated logs.

test: This directory contains various test


cases.

vendor: This directory contains composer


dependencies.

Console: All the artisan commands are


stored in this directory.

Exceptions: This directory contains your


application's exception handler
MVC Architecture
❑M: 'M' stands for Model.

A model is a class that deals with a database.

For example, if we have users in an application then we will have users model that
deals with a database to query the table of users if we have users model, then we will
also have a users table.

We conclude from the example that the model is going to have a table for that specific
model.

❑V: 'V' stands for View.

A view is a class that deals with an HTML. Everything that we can see on the application
in the browser is the view or the representation.

❑C: 'C' stands for Controller.

A controller is the middle-man that deals with both model and view.
A controller is the class that retrieves the data from the model and sends the data to
the view class.
MVC Architecture
Routing in Laravel
❑Routing means, route your request to an appropriate controller.
❑The routes of the application can be defined in routes/web.php file.
❑Controller play important role between View and Model.
❑General route Syntax:
routes/web.php
Routing Parameters

Some times we need to capture the parameters passed with the URL.
To do this, we need to modify the code in web.php

❑Required Parameters
❑Optional Parameters

Required Parameters

✔These parameters must be present in the URL.


✔For example, if you want to capture the ID from the URL. The sample coding is

Route :: get ('emp/{id}', function ($id) {


echo 'Employee ID is'. $id; });

✔Whatever argument that we pass after the root URL (http://localhost:8000/emp/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

✔Many parameters do not remain present within the URL, but the developers had to
use them.
✔Such parameters get indicated by a "?" (question mark sign) following the
parameter's name.
✔The presence of these parameters is not necessary in the URL.

Route :: get (‘emp/{name?}', function ($name = ‘Sandip') {


echo $name; });
Controller
❑Controller are class based php files
❑Controllers can group related request handling logic into a single class.
❑Controller play important role between View and Model.
❑ Controllers directs traffic among the Views and the Models.

Types of Controller
❑Basic Controller
✔Simple controller without built-in methods.
❑Resource Controller
✔Controller with built-in methods.
✔Used for CRUD Operations

Creating a Controller

php artisan make : controller <controller-name> Basic Controller

php artisan make : controller <controller-name> --resource Resource Controller

The created controller can be seen at app/Http/Controllers.


Controller - View - users
mycontroller.php

Call the Controller from routes - web.php


Artisan
❑Command line interface for Laravel.
❑It provides a number of helpful commands for your use while developing your
application.
❑Its like REPL in Node JS.

Listing All Available Commands

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

Viewing The Help Screen For A Command

php artisan help migrate

Displaying Your Current Laravel Version

php artisan --version

To start Laravel
project
php artisan serve
PHP Artisan Commands
Composer
❑Composer is a dependency manager for a PHP programming language that
manages the dependencies of PHP software and required libraries.
❑In Laravel, the composer is a tool that includes all the dependencies and
libraries.
❑Third-party libraries can be installed easily using composer.
❑It helps us to manage libraries/packages required for our projects.
❑Composer is used to managing its dependencies and the dependencies are
noted in composer.json file which is placed in the source folder.
❑Composer runs through the command line.
❑The main purpose of the composer is to install
the dependencies or libraries for an application.

Add packages to your composer.json

composer require firebase/php-jwt

Here , firebase/php-jwt is the name of the Package


Remove Package from Laravel Application

❑Use the following command to remove the package from vendor.


❑This will also update composer.json and composer.lock accordingly.

Syntax : composer remove vendor/package

Example : composer remove firebase/php-jwt

composer remove vendor/package Composer.json

Updating Packages

composer update monolog/monolog


Cookies
❑Cookies are small blocks of data that are created by web servers while users are
browsing a website.
❑These are stored by the web browser on the user’s computer..
❑Cookies data remain until they expire or are deleted.
❑Cookies are useful for authentication processes, as well as for storing information
about user preferences for a site.
❑There can be more than one cookie on the user’s device during a session.
❑Sometimes, we need to store some information in the user's browser and then we
need to do some tasks based on that information, at that time we need to use
cookie.

Creating a Cookie
❑The cookie can be attached to the response using the withCookie() method.
❑Create a response instance of Illuminate\Http\Response class to call the
withCookie() method.
❑Cookie generated by the Laravel are encrypted and signed and it can’t be
modified or read by the client.
//Create a response instance

$response = new Illuminate\Http\Response('Hello World');

//Call withCookie() method with the response method

$response-> withCookie(cookie('name', 'value', $minutes));

//return the response

return $response;

❑Cookie() method will take 3 arguments.


❑First argument is the name of the cookie, second argument is the value of the
cookie and the third argument is the duration of the cookie after which the cookie
will get deleted automatically.
❑Cookie can be set forever by using the forever method as shown in the below code.

$response->withCookie(cookie()->forever('name', 'value'));
Retrieving a Cookie

❑Once we set the cookie, we can retrieve the cookie by cookie() method.
❑This cookie() method will take only one argument which will be the name of the cookie.
❑The cookie method can be called by using the instance of Illuminate\Http\Request.

Here is a sample code.

//’name’ is the name of the cookie to retrieve the value

$value = $request->cookie('name');

Creating A Cookie That Lasts Forever

$response->withCookie(cookie()->forever('name', 'value'));
Session
❑Sessions are used to store information about the user across the requests.

Storing Session Data


❑Data can be stored in session using the put() method.
❑The put() method will take two arguments, the “key” and the “value”.

$request->session()->put('key', 'value');

Deleting Session Data


❑The forget() method is used to delete an item from the session.
❑This method will take “key” as the argument.

$request->session()->forget('key');

❑Use flush() method instead of forget() method to delete all session data.
Accessing Session Data

❑To access the session data, we need an instance of session which can be
accessed via HTTP request.
❑After getting the instance, we can use the get() method, which will take one
argument, “key”, to get the session data.

$value = $request->session()->get('key');

❑You can use all() method to get all session data instead of get() method.

$request->session()->all();

Determining if an Item Exists in the Session

$request->session()->has(‘key’);

You might also like