0% found this document useful (0 votes)
6 views

UNIT 1 Laravel (1) (1)

This document provides an introduction to Laravel, an open-source PHP framework that utilizes the MVC architecture to streamline web development tasks such as authentication, routing, and caching. It covers key features of Laravel, installation steps, and the application structure, including directories for configuration, middleware, and service providers. The document aims to familiarize students with the basic concepts and functionalities of Laravel to enhance their development experience.

Uploaded by

vishalroy25740
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

UNIT 1 Laravel (1) (1)

This document provides an introduction to Laravel, an open-source PHP framework that utilizes the MVC architecture to streamline web development tasks such as authentication, routing, and caching. It covers key features of Laravel, installation steps, and the application structure, including directories for configuration, middleware, and service providers. The document aims to familiarize students with the basic concepts and functionalities of Laravel to enhance their development experience.

Uploaded by

vishalroy25740
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 81

UNIT 1

INTRODUCTION TO LARAVEL
Unit Content

• Introduction to Laravel, Laravel Features, Laravel installation, Application


Structure of Laravel, Root Directory, App Directory, Basic
Configuration, Environmental Configuration, Routing, Routing
Parameters, Middleware, Terminable Middleware, Middleware
Parameter, Controllers, Restful Resource Controllers, Implicit
Controllers, Constructor Injection, Method Injection, Laravel Sail,
Laravel Jetstream.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 2
Unit I
Unit Objective

• In this unit we will learn basic of Laravel.


• Laravel attempts to take the pain out of development by easing common
tasks used in the majority of web projects, such as authentication,
routing, sessions, and caching.
• Laravel aims to make the development process a pleasing one for the
developer without sacrificing application functionality.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 3
Unit I
Topic Objective

• Topic :Introduction to Laravel


In this topic, the students will learn about the basic of Laravel. And also basic
of Laravel framework and MVC.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 4
Unit I
Introduction to Laravel

• Laravel is an open-source PHP framework. It also offers the rich set of


functionalities that incorporates the basic features of PHP frameworks such as
Codelgniter, and other programming languages like Ruby on Rails.
What is Laravel?
• Laravel is a PHP framework that uses the MVC architecture.
where,
• Framework: It is the collection of methods, classes, or files that the programmer
uses, and they can also extend its functionality by using their code.
• Architecture: It is the specific design pattern that the framework follows. Laravel is
following the MVC architecture.

A framework is like a structure that provides a base for the application development process. With the help of a framework, you can
avoid writing everything from scratch. Frameworks provide a set of tools and elements that help in the speedy development process

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 5
Unit I
Laravel Features

• We know that PHP is the oldest programming language used by the programmers,
and more than 20 million websites are developed by using PHP. PHP is a very
suitable programming language as it satisfies the business requirements whether
the business is big or small. Laravel is one of the most popular frameworks having a
high rich set of functionalities.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 6
Unit I
Continue..

• 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.
• Innovative Template Engine
• Laravel provides an innovative template engine which allows the
developers to create the dynamic website. The available widgets in
Laravel can be used to create solid structures for an application.
• Effective ORM (Object Relational Mapper)
• Laravel contains an inbuilt ORM with easy PHP Active Record
implementation. An effective ORM allows the developers to query
the database tables by using the simple PHP syntax without writing
any SQL code. It provides easy integration between the developers
and database tables by giving each of the tables with their
corresponding models.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 7
Unit I
Continue..

MVC Architecture Support


• Laravel supports MVC architecture. It provides faster
development process as in MVC; one programmer can work on the
view while other is working on the controller to create the business
logic for the web application. It provides multiple views for a
model, and code duplication is also avoided as it separates the
business logic from the presentation logic.
Secure Migration System
• Laravel framework can expand the database without allowing the
developers to put much effort every time to make changes, and the
migration process of Laravel is very secure and full-proof. In the
whole process, php code is used rather than SQL code.
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.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 8
Unit I
Continue..

Intact Security
• Application security is one of the most important factors in
web application development. While developing an application, a
programmer needs to take effective ways to secure the application.
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 and Modular
• 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 (Cross-Site Request Forgery) protection. This
framework is divided into several modules that follow the php
principles allowing the developers to build responsive and modular
apps
Rajat Kumar Vue.js Framework & Inertia.js
2/22/2023 9
Unit I
Continue..

• Artisan
• Laravel framework provides a built-in tool for a command-line
known as Artisan that performs the repetitive programming tasks
that do not allow the php developers to perform manually. These
artisans can also be used to create the skeleton code, database
structure, and their migration, so it makes it easy to manage the
database of the system. It also generates the MVC files through the
command line. Artisan also allows the developers to create their
own commands.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 10
Unit I
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.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 11
Unit I
Laravel installation

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 12
Unit I
Laravel installation

• 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


• 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 −

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 13
Unit I
Laravel installation

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 14
Unit I
Laravel installation

• 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 −

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 15
Unit I
Laravel installation

• 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.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 16
Unit I
Topic objective

• Application Structure of Laravel


In this topic student will understand the structure of Laravel,
directory of Laravel and bootstrap directory.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 17
Unit I
Application Structure of Laravel

• 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 −

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 18
Unit I
Root Directory

• Application Structure

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 19
Unit I
Continue..

• App
• It is the application folder and includes the entire source code of
the project. It contains events, exceptions and middleware
declaration. The app folder comprises various sub folders as
explained below −
• 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.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 20
Unit I
Continue..

• 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
• 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.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 21
Unit I
Continue..

• Laravel's app directory


• The app folder is one of the major folders in Laravel as most of the
code is written in the app folder. The App folder contains the
following sub-folders:
• Console
• Exceptions
• Http
• Providers

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 22
Unit I
Continue..

• Console
• Console folder contains the artisan commands required for Laravel.
It contains the commands which are declared with the appropriate
signature.
• Exceptions
• Exceptions folder contains the various exception handlers. It
handles the exceptions thrown by the Laravel project. The
Exceptions directory contains the methods that handle the
exceptions.
• The Exceptions directory contains the file handle.php that handles all
the exceptions.
• Http
• The http folder is a sub-folder of the app folder. It has sub-folders
such as controllers, middleware, and requests. Laravel follows the
MVC architecture, so http includes controllers, views, and requests.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 23
Unit I
Continue..

• Where,
• Middleware: It is a sub-folder of the http directory. It provides a filter
mechanism and communication between request and response.
• Requests: It is a sub-folder of http which includes all the requests of
an application.
• Providers
• The Providers directory is used to contain all the service providers
that are required to register events for core servers and provides
configuration for Laravel application.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 24
Unit I
Continue..

• Laravel's bootstrap directory


• The bootstrap directory holds the files that are required to
bootstrap the Laravel application and to configure auto-loading.
The bootstrap folder contains a sub-folder cache used for caching
a web application. It also contains the file app.php that
initializes the scripts required for bootstrap.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 25
Unit I
Continue..

• The above screen shows the structure of the bootstrap directory.


It contains one
folder, i.e., cache and two files, app.php and autoload.php.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 26
Unit I
Continue..

• Laravel's config directory


• The config's directory contains the various configuration files
required for the Laravel application. Various files are available
inside the config's directory shown in the below screenshot, and
each file performs their functionalities as per their names.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 27
Unit I
Topic Objective

Topic :Basic Configuration


In this topic student will gain about basic configuration with Laravel and also
how environmental configuration works.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 28
Unit I
Basic Configuration

• All of the configuration files for the Laravel framework are stored in the config
directory. Each option is documented, so feel free to look through the files and get
familiar with the options available to you.
• These configuration files allow you to configure things like your database connection
information, your mail server information, as well as various other core configuration
values such as your application timezone and encryption key.
• Application Overview
• We can get a quick overview of your application's configuration, drivers, and
environment via the about Artisan command:

php artisan about

• If you're only interested in a particular section of the application overview output,


you may filter for that section using the --only option:

php artisan about --only=environment

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 29
Unit I
Environmental Configuration

Environment Configuration
• Environment Variable Types
• Retrieving Environment Configuration
• Determining The Current Environment
• Encrypting Environment Files

• Environment Configuration
• It is often helpful to have different configuration values based on the environment
where the application is running. For example, you may wish to use a different cache
driver locally than you do on your production server.
• To make this a effective, Laravel utilizes the DotEnv PHP library. In a fresh Laravel
installation, the root directory of your application will contain a .env.example file that
defines many common environment variables. During the Laravel installation process,
this file will automatically be copied to .env.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 30
Unit I
Continue..

• Environment Variable Types


• All variables in your .env files are typically parsed as strings, so
some reserved values have been created to allow you to return a
wider range of types from the env() function:
.env Value env() Value
true (bool) true
(true) (bool) true
false (bool) false
(false) (bool) false
empty (string) ''
(empty) (string) ''
null (null) null
(null) (null) null

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 31
Unit I
Continue..

• If you need to define an environment variable with a value that contains spaces,
you may do so by enclosing the value in double quotes:

APP_NAME="My Application"

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 32
Unit I
Continue..

• Retrieving Environment Configuration


• All of the variables listed in the .env file will be loaded into the $_ENV PHP super-
global when your application receives a request. However, you may use the env
function to retrieve values from these variables in your configuration files. In fact, if
you review the Laravel configuration files, you will notice many of the options are
already using this function:

'debug' => env('APP_DEBUG', false),

• The second value passed to the env function is the "default value". This value will
be returned if no environment variable exists for the given key.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 33
Unit I
Continue..

• Determining The Current Environment


• The current application environment is determined via the APP_ENV variable from your .env file. You may access
this value via the environment method on the App facade:

• use Illuminate\Support\Facades\App;

• $environment = App::environment();

• You may also pass arguments to the environment method to determine if the environment matches a given value.
The method will return true if the environment matches any of the given values:

• if (App::environment('local')) {
• // The environment is local
• }

• if (App::environment(['local', 'staging'])) {
• // The environment is either local OR staging...
• }

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 34
Unit I
Continue..

• Encrypting Environment Files


• Unencrypted environment files should never be stored in source control. However,
Laravel allows you to encrypt your environment files so that they may be safely be
added to source control with the rest of your application.

• Encryption
• To encrypt an environment file, you may use the env:encrypt command:

• php artisan env:encrypt

• Running the env:encrypt command will encrypt your .env file and place the
encrypted contents in an .env.encrypted file. The decryption key is presented in the
output of the command and should be stored in a secure password manager. If you
would like to provide your own encryption key you may use the --key option when
invoking the command:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 35
Unit I
Continue..

• php artisan env:encrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF

• The length of the key provided should match the key length required by the
encryption cipher being used. By default, Laravel will use the AES-256-CBC cipher
which requires a 32 character key. You are free to use any cipher supported by
Laravel's encrypter by passing the --cipher option when invoking the command.

• If your application has multiple environment files, such as .env and .env.staging, you
may specify the environment file that should be encrypted by providing the
environment name via the --env option:

• php artisan env:encrypt --env=staging

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 36
Unit I
Topic Objective

Topic :Routing
Routing in Laravel allows the students to route all your
application requests to its appropriate controller.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 37
Unit I
Routing

Routing is one of the essential concepts in Laravel. The main functionality of the
routes is to route all your application requests to the appropriate controller.

Default Route files


All Laravel routes are defined inside the route files located in
the routes directory.
When we create a project, then a route directory is created
inside the project. The route/web.php directory contains the
definition of route files for your web interface.
The routes in web.php are assigned with the web
middleware group that provides the features like session
state and CSRF protection.
The routes defined in routes/api.php are assigned with the
API middleware
group, and they are stateless.

We will start by defining the routes in routes/web.api file.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 38
Unit I
Continue..

<?php
Route::get('/', function ()
{
return view ('welcome');
});

In the above case, Route is the class which defines the


static method get(). The get() method contains the
parameters '/' and function() closure. The '/' defines the root
directory and function() defines the functionality of the get()
method.

In the above route, the url is '/'; therefore,


we entered the localhost/laravelproject/public URL in the web
browser.

As the method returns the view('welcome'), so the above


output shows the
welcome view of the Laravel.
Rajat Kumar Vue.js Framework & Inertia.js
2/22/2023 39
Unit I
Continue..

<?php
Route::get('/example',
function ()
{
return "Hello World";
});

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 40
Unit I
Continue..

• The routing mechanism is shown in the image given


below −

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 41
Unit I
Continue..

• Let us now understand the steps involved in routing mechanism in detail



• Step 1 − Initially, we should execute the root URL of the application.
• Step 2 − Now, the executed URL should match with the
appropriate method in the route.php file. In the present case, it should
match 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. Next, the
function calls the view() function with argument ‘welcome’ without using
the blade.php.
• This will produce the HTML output as shown in the image below −

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 42
Unit I
Routing Parameters

• There are two types of parameters we can use:


• Required Parameters
• Optional Parameters

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 43
Unit I
Continue..

• Required Parameters
• The required parameters are the parameters that we pass in the URL. Sometimes
you want to capture some segments of the URI then this can be done by passing the
parameters to the URL. For example, you want to capture the user id from the URL.
• Output
<?php • When we enter the URL "localhost/laravelproject/public/".
Route::get('/', function()
{
return "This is a home page";
}
);
Route::get('/about', function()
{
return "This is a about us page";
}
);
Route::get('/contact', function()
{
return "This is a contact us page";
}
);

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 44
Unit I
Continue..

• When we enter the URL "localhost/laravelproject/public/about".

• When we enter the URL "localhost/laravelproject/public/contact".

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 45
Unit I
Optional Parameters

• Suppose you want to specify the route parameter occasionally, in


order to achieve this, you can make the route parameter
optional. To make the route parameter optional, you can place '?'
operator after the parameter name. If you want to provide the
optional parameter, and then make sure that you have also
provided the default value to the variable.

•Let's understand through some


examples. Example 1:
Route::get('user/{name?}', function
($name=null) { return $name;
});

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 46
Unit I
Continue..

• When we do not pass any variable to the URL, then the output would be:

• When we pass 'akshita' in the URL, then the output would be:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 47
Unit I
Continue..

• From the above outputs, we observe that the parameter we


pass in the URL is optional. As we have provided the default value
to the parameter as Null, so if we do not pass any parameter, it will
return null. If we pass the parameter in the URL, then the value of
the parameter would be displayed.
Route::get('user/{name?}', function ($name
= 'himani') { return $name;
});
• In the above example, we have provided
the default value as 'himani’.
• OUTPUT

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 48
Unit I
Middleware

Topic : Middleware
In this topic student understand that how Middleware
provide a convenient mechanism for inspecting and
filtering HTTP requests entering your application. For
example, Laravel includes a middleware that verifies the user
of your application is authenticated.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 49
Unit I
Middleware

Middleware acts as a layer between the user and the request. It


means that when the user requests the server then the request
will pass through the middleware, and then the middleware
verifies whether the request is authenticated or not. If the user's
request is authenticated then the request is sent to the backend.
If the user request is not authenticated, then the middleware
will redirect the user to the login screen.

An additional middleware can be used to perform a variety of


tasks except for authentication. For example, CORS (Cross Origin
Resource Sharing) middleware is responsible for adding headers
to all the responses.

Laravel framework includes several middleware such as


authentication and CSRF protection, and all these are located in
the app/Http/Middleware directory.
We can say that middleware is an http request filter where you
can check the
conditions. Rajat Kumar Vue.js Framework & Inertia.js
2/22/2023 50
Unit I
Middleware Parameters

In middleware, we are going to discuss the


following topics:

• Make a middleware
• Apply middleware
• Check condition in middleware
• Route middleware

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 51
Unit I
Creating a Middleware

Type the command php artisan make:middleware 'name of


the middleware'.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 52
Unit I
Creating a Middleware

In the above screen, we type the command "php artisan make:middleware


CheckAge" where CheckAge is the name of the middleware. The above window
shows that the middleware has been created successfully with the name
"CheckAge".

To see whether the CheckAge middleware is created or not, go to your project.


Our project name is laravelproject, so the path for the middleware would
be: C:\xampp\htdocs\laravelproject\app\Http\Middleware.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 53
Unit I
Apply a Middleware

Middleware can be either applied to all the URLs or some particular URLs.

Let's apply the middleware to all the URLs.

Step 1: Open the kernel.php file. If we want to apply the middleware to all the
URLs, then add the path of the middleware in the array of middleware.
Step 2: Type the command php artisan serve in Git Bash Window.
Step 3: Open the CheckAge.php file, which you have created as a middleware.

Step 4: Now, enter the URL 'http://localhost/laravelproject/public/'.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 54
Unit I
Apply a Middleware

Let's apply the middleware to some specific routes.

Step 1: Open the kernel.php file. If we want to apply the middleware to some
specific routes

Step 2: Open the CheckAge.php file, which you have created as a


middleware.

Step 3: Add the middleware code in the web.php file.

Route::Get('/',function()
{
return view('welcome');
})-> middleware('age');
Route::Get('user/profile',function()
{
return "user profile";
});

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 55
Unit I
RESTful Resource Controllers

Laravel resource controllers provide the CRUD routes to the controller in a single
line of code. A resource controller is used to create a controller that handles all
the http requests stored by your application.
The resource() is a static function like get() method that gives access to multiple
routes that we can use in a controller.
Syntax of resource() method:
Route::resource('posts','PostController');

In the above syntax, 'posts' contains all the routes, and 'PostController' is the
name of the controller. In this case, we do not need to specify the method name
such as @index as we did in get() method because create(), store(), destroy()
methods are already available in the PostController class.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 56
Unit I
RESTful Resource Controllers

Step 1: Create the controller by using the command given below:

php artisan make:controller PostController -resource;

The above command will create the Controller at


the app/Http/Controllers/PostController.php directory. The PostController class
contains the methods for each resource operations.

Step 2: Now, we need to register the resourceful route to the Controller, and
which can be done as follows:

Route::resource('posts','PostController');

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 57
Unit I
RESTful Resource Controllers
Open the Git Bash Window, and enter the command php artisan route:list.
This command produces the following output:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 58
Unit I
RESTful Resource Controllers

The post parameter in the resource() method produces the names or


resources shown in the above output, and its corresponding methods. In the
above output, the posts.destroy is sending a parameter to the Delete method,
which is very special in Laravel.

Let's understand the concept of resources through an example.

Accessing the show() method of PostController class

Suppose we want to call the show() method of PostController.php file.


To do so, add the code in show() method. I added the following code in
show() method:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 59
Unit I
RESTful Resource Controllers

public function show($id)


{
return "show method is called and ID is : ". $id
}

As we know that URI of the posts.show is posts/{posts}, which means that


we need to enter the parameter as well to access the show() method of
the PostController class.

Suppose I entered the URL as 'localhost/laravelproject/public/posts/58',


then the output would be:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 60
Unit I
RESTful Resource Controllers

Accessing the create() method of PostController class

Step 1: First, we need to add the code in create() method.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 61
Unit I
RESTful Resource Controllers

public function create()


{
return "This is the create method";
}

As we know that the URI of the posts.create is posts/create, so the URL to


access the create() method would be

'localhost/laravelproject/public/posts/create'.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 62
Unit I
RESTful Resource Controllers

Step 2: Enter the URL 'localhost/laravelproject/public/posts/create' to the


browser, then the output would be:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 63
Unit I
RESTful Resource Controllers

Registering routes for multiple controllers

We can register the routes for multiple controllers by passing an array to


the resources() method. Suppose I want to register the routes for two
controllers, such as PostController and StudentController. Following are the
steps to achieve this:

Step 1: First, you need to create


the PostController and StudentController by using the following commands:

Php artisan make:controller PostController;

// to create the PostController.

Php artisan make:controller StudentController;


// to create the StudentController.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 64
Unit I
RESTful Resource Controllers

Step 2: Add the code given below in web.php file to register routes:

route::resources( ['posts'=>'Pos
tController',
'student'=>'StudentController']
);

Step 3: Enter the command php


artisan route:list on Git Bash
Window.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 65
Unit I
RESTful Resource Controllers

The above screen shows that routes of both the PostController and StudentController
are registered.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 66
Unit I
Dependency Injection & Controllers

Constructor Injection:

The Laravel service container is used to resolve all Laravel controllers. As a


result, you are able to type-hint any dependencies your controller may need
in its constructor. The declared dependencies will automatically be resolved
and injected into the controller instance:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 67
Unit I
Constructor Injection:

<?php

namespace App\Http\Controllers;

use App\Repositories\UserRepository;

class UserController extends Controller


{
/**
* Create a new controller instance.
*/
public function
construct( protected
UserRepository $users,

) {}
}

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 68
Unit I
Method Injection

In addition to constructor injection, you may also type-hint


dependencies on your controller's methods.

A common use-case for method injection is injecting the


Illuminate\Http\Request instance into your controller methods:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 69
Unit I
Cont…

<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*/
public function store(Request $request):
RedirectResponse
{
$name = $request->name;
// Store the user...
return redirect('/users');
}
}

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 70
Unit I
Cont…

If your controller method is also expecting input from a route parameter, list
your route arguments after your other dependencies. For example, if your
route is defined like so:

use App\Http\Controllers\UserController;

Route::put('/user/{id}', [UserController::class, 'update']);

You may still type-hint the Illuminate\Http\Request and


access your id
parameter by defining your controller
method as follows:

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 71
Unit I
Cont…

<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class UserController extends Controller


{
/**
* Update the given user.
*/
public function update(Request
$request,
string $id): RedirectResponse
{
// Update the user...

return redirect('/users');
}
Rajat Kumar Vue.js Framework & Inertia.js
2/22/2023 } Unit I
72
Laravel Sail

• Laravel Sail is a light-weight command-line interface for


interacting with Laravel's default Docker development
environment. Sail provides a great starting point for building a
Laravel application using PHP, MySQL, and Redis without requiring
prior Docker experience.

• At its heart, Sail is the docker-compose.yml file and the sail script
that is stored at the root of your project. The sail script provides
a CLI with convenient methods for interacting with the Docker
containers defined by the docker-compose.yml file.

• Laravel Sail is supported on macOS, Linux, and Windows (via


WSL2).

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 73
Unit I
Cont…

Laravel Sail is automatically installed with all new Laravel applications so you
may start using it immediately. To learn how to create a new Laravel
application, please consult Laravel's installation documentation for your
operating system.

During installation, you will be asked to choose which Sail supported


services
your application will be interacting with.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 74
Unit I
Installing Sail Into Existing Applications

If you are interested in using Sail with an existing Laravel application, you
may simply install Sail using the Composer package manager. Of course,
these steps assume that your existing local development environment
allows you to install Composer dependencies:

composer require laravel/sail --dev


After Sail has been installed, you may run the sail:install Artisan command.
This command will publish Sail's docker-compose.
yml file to the root of your application:

php artisan sail:install

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 75
Unit I
Cont…

Finally, you may start Sail. To continue learning how to use Sail, please
continue reading the remainder of this documentation:

./vendor/bin/sail up

Adding Additional Services


If you would like to add an additional service
to your existing Sail installation,
you may run the sail:add Artisan command:

php artisan sail:add

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 76
Unit I
Laravel Jetstream

Laravel Jetstream is a beautifully designed application starter kit


for Laravel
and provides the perfect starting point for your next Laravel
application. Jetstream provides the implementation for your
application's login, registration, email verification, two-factor
authentication, session management, API via Laravel Sanctum,
and optional team management features.
Jetstream is designed using Tailwind CSS and offers your
choice of Livewire or Inertia scaffolding

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 77
Unit I
Laravel Jetstream

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 78
Unit I
Laravel Jetstream

Available Stacks

Laravel Jetstream offers your choice of two frontend


stacks: Livewire and Inertia.js. Each stack provides a
productive, powerful starting point for building your
application; however, the stack you choose will depend
on your preferred templating
language.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 79
Unit I
Laravel Jetstream

Livewire + Blade

Laravel Livewire is a library that makes it simple to build


modern, reactive, dynamic interfaces using Laravel Blade
as your templating language.

This is a great stack to choose if you want to build an


application that is dynamic and reactive, and is a great
alternative to a full JavaScript framework like Vue.js.

When using Livewire, you may pick and choose which


portions of your application will be a Livewire
component, while the remainder of your application can
be rendered as the traditional Blade templates you are
used to.

Rajat Kumar Vue.js Framework & Inertia.js


2/22/2023 80
Unit I
Laravel Jetstream

Inertia + Vue

The Inertia stack provided by Jetstream uses Vue.js as its


templating language. Building an Inertia application is a lot like
building a typical Vue application; however, you will use
Laravel's router instead of Vue router.
Inertia is a small library that allows you to render single-file
Vue components from your Laravel backend by providing the
name of the component and the data that should be hydrated
into that component's "props".
In other words, this stack gives you the full power of Vue.js
without the complexity of client-side routing. You get to use
the standard Laravel routing and view data hydration
approaches that you are used to.
The Inertia stack is a great choice if you are comfortable with
and enjoy
using Vue.js as your templating language.
Rajat Kumar Vue.js Framework & Inertia.js
2/22/2023 81
Unit I

You might also like