Introduction to Laravel
Lectures 8 and 9
What is Laravel?
• Laravel is a free, open-source PHP framework following
the Model-View-Controller (MVC) architectural pattern
built on top of Symphony2
• Most popular framework in GitHub
• Laravel features:
– Modularity
– Routing
– Template engine
– Sessions and authentications
– Query Builder and ORM
– Database Migrations
How to install Laravel?
• Make sure that in terminal php -v command is
working
– if not set PATH="…;C:\XAMPP\php"
• Download Composer from
http://getcomposer.org/download
• In a folder where you would like to create a
Laravel project, run following command:
– composer create-project laravel/laravel project-name
• This will create a folder with Laravel project in it
How to start Laravel project?
• Go to a folder with Laravel project and run following
command in terminal:
– php artisan serve
• This will start a standalone Laravel development server in
http://localhost:8000
• Give edit permissions to following folders inside Laravel
project:
– storage
– bootstrap/cache
• In Linux-based OS, following commands can be executed in
terminal:
– chmod 775 storage
– chmod 775 bootstrap/cache
Laravel project structure
• app - This directory contains the core code
of the application.
• bootstrap - This directory contains the
application bootstrapping script.
• 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. It also
contains the assets of the application like
JavaScript, CSS,Images, etc
Laravel project structure
• resources - This directory contains raw assets such
as the LESS and Sass files, localization and language
files, and Templates that are rendered as HTML.
• 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.
• .env file - Contains all environment variables used
to configure Laravel project
• composer.json - Contains information about all
PHP dependencies of Laravel project
• package.json - Contains information about all
JavaScript dependencies of Laravel project
Laravel app project structure
• Console - All the artisan commands are stored
in this directory.
• Events - This directory stores events that your
application can raise. Events may be used to
alert other parts of your application that a
given action has occurred, providing a great
deal of flexibility and decoupling.
• Exceptions - This directory contains your
application’s exception handler and is also a
good place to stick any exceptions thrown by
your application.
• Http - This directory contains your controllers,
filters, and requests.
Artisan Toolkit
• Artisan Commandline Toolkit helps to organize
development process in Laravel project.
• For example:
– Creates different application components
– Manages database migrations and seeds
– Creates utility commands and executes tasks
– Check application routes
– And many more…
Artisan Toolkit
• php artisan list
Model-View-Controller (MVC)
Architecture
Model-View-Controller (MVC)
Architecture
• MVC is an architectural pattern that decouples user interface from
data representation.
• MVC is not only used in Web, but in other platforms as well.
• In Laravel's MVC,
– View is an HTTP Response containing HTML and CSS to be rendered in
browser
– Model is a class representing data and bahaviour of domain's
abstractions (entities)
– Controller is a class responding to user actions via interacting with
corresponding models and selecting corresponding views
– Router is a class converting HTTP Requests into user actions
implemented by a Controller
– Middleware is a class that stands between Router and Controller to
process HTTP requests before they trigger corresponding action
Views
• Views contain HTML served by your
application and separate your application logic
from your presentation logic
• Views are stored in:
– resources/views
• Views use Blade template engine to generate
dynamic HTML content from static templates
Blade - Template Engine
• Views are created using Blade template
engine
• Blade does not restrict use of plain PHP code
in views
• Blade templates end with
.blade.php
and
stored in
resources/views
• Blade templates are cached into plain PHP
code and therefore create zero overhead in
generating dynamic HTML content
Blade Layouts
• Layouts are templates with placeholder sections
that can be filled either with other templates or
data.
• @yield('placeholder_name')
directive is used to
define a section inside a layout
• @extend('layout')
directive is used indicate the
placeholders of which layout are going to be filled
• @section('placeholder_name')… @endsection
provides content to be pasted inside given
placeholder of chosen layout
Templating Engine
Embedding in Blade Template
Embedding variable
Embedding section inside layout
Embedding in Blade Template
Embedding partials
Filling slot inside component
alert.blade.php
Read:
https://laravel.com/docs/5.8/blade
Router: Handling Get Request
• Router maps HTTP requests to corresponding action
handlers
• Action handlers can be set directly inside
routes/web.php
as function closures, or
implemented inside certain controller.
Router: Sending Response
Router: Handling Post Request
Read:
https://laravel.com/docs/5.8/routing
Dependency Injection
• A special class called
Do'stlaringiz bilan baham: |