UNIT 1 Laravel (1) (1)
UNIT 1 Laravel (1) (1)
INTRODUCTION TO LARAVEL
Unit Content
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
• 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.
• 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.
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.
• 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.
• Application Structure
• 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.
• 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.
• 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.
• 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.
• 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:
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.
• 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"
• 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.
• 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...
• }
• Encryption
• To encrypt an environment file, you may use the env:encrypt command:
• 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:
• 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:
Topic :Routing
Routing in Laravel allows the students to route all your
application requests to its appropriate controller.
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.
<?php
Route::get('/', function ()
{
return view ('welcome');
});
<?php
Route::get('/example',
function ()
{
return "Hello World";
});
• 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";
}
);
• 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:
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.
• Make a middleware
• Apply middleware
• Check condition in middleware
• Route middleware
Middleware can be either applied to all the URLs or some particular 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 1: Open the kernel.php file. If we want to apply the middleware to some
specific routes
Route::Get('/',function()
{
return view('welcome');
})-> middleware('age');
Route::Get('user/profile',function()
{
return "user profile";
});
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.
Step 2: Now, we need to register the resourceful route to the Controller, and
which can be done as follows:
Route::resource('posts','PostController');
'localhost/laravelproject/public/posts/create'.
Step 2: Add the code given below in web.php file to register routes:
route::resources( ['posts'=>'Pos
tController',
'student'=>'StudentController']
);
The above screen shows that routes of both the PostController and StudentController
are registered.
Constructor Injection:
<?php
namespace App\Http\Controllers;
use App\Repositories\UserRepository;
) {}
}
<?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');
}
}
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;
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
return redirect('/users');
}
Rajat Kumar Vue.js Framework & Inertia.js
2/22/2023 } Unit I
72
Laravel Sail
• 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 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.
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:
Finally, you may start Sail. To continue learning how to use Sail, please
continue reading the remainder of this documentation:
./vendor/bin/sail up
Available Stacks
Livewire + Blade
Inertia + Vue