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

Basic Routing 1

Uploaded by

Kaushal Anand
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)
24 views

Basic Routing 1

Uploaded by

Kaushal Anand
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/ 58

BASIC ROUTING

By default laravel project runs on


which PORT?

• 3000

• 5000

• 8000

• 7000
By default laravel project runs on
which PORT?

• 3000

• 5000

• 8000

• 7000
Command to check the current version of
the Laravel Framwork?

• php artisan main --version

• php artisan --version status

• php artisan check --version

• php artisan --version


Command to check the current version of
the Laravel Framwork?

• php artisan main --version

• php artisan --version status

• php artisan check --version

• php artisan --version


What is the purpose of the web.php
routes file in Laravel?

• a) It defines routes for web application routes


and middleware.
• b) It defines routes specifically for API
endpoints.
• c) It defines routes for console commands and
tasks.
• d) It defines routes for unit testing
What is Routing?

The process of directing incoming


requests (usually based on URLs)
to the appropriate code or handler
that will handle the request and
generate a response.
Routing is like a traffic director for web
applications. When a user enters a
URL in their browser or clicks a link,
routing decides where that request
should go within the application to
generate the appropriate content or
perform a specific action.
URL

URL stands for "Uniform Resource


Locator." It's a reference or address used to
access resources on the internet. In simpler
terms, a URL is the unique web address
that you enter into a web browser to
access a specific web page, file, or
resource.
use Illuminate\Support\Facades\Route;
Route::get('/', function ()
{
return view ('Hello‘);
});

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.
Paamayim Nekudotayim ::
It is a token that allows access to static,
constant, and overridden properties or methods
of a class. When referencing these items from
outside the class definition, use the name of
the class.

It's possible to reference the class using a


variable. The variable's value can not be a
keyword (e.g. self, parent and static).
Do you know?

Paamayim Nekudotayim would, at first,


seem like a strange choice for naming
a double-colon. However, while writing
the Zend Engine 0.5 (which powers
PHP 3), that's what the Zend team
decided to call it. It actually does mean
double-colon - in Hebrew!
The Default Route Files
• All Laravel routes are defined in your route files, which are
located in the routes directory.

• These files are automatically loaded by your application's


App\Providers\RouteServiceProvider.

• The routes/web.php file defines routes that are for your


web interface. These routes are assigned the web
middleware group.
What is the purpose of the web.php
routes file in Laravel?

• a) It defines routes for web application


routes and middleware.
• b) It defines routes specifically for API
endpoints.
• c) It defines routes for console commands and
tasks.
• d) It defines routes for unit testing
The Default Route Files(contd.)
• For most applications, you will begin by defining routes in
your routes/web.php file.

• The routes defined in routes/web.php may be accessed


by entering the defined route's URL in your browser.

• For example, you may access the following route by


navigating to http://localhost:8000/greetings in your
browser
Command to get the route
list in Laravel

• php artisan route:list


In Laravel, where are the routes
primarily defined?

• a) In the app/routes.php file


• b) In the routes folder within the app
directory
• c) In the .env configuration file
• d) In the web.php or api.php files within
the routes directory
In Laravel, where are the routes
primarily defined?

• a) In the app/routes.php file


• b) In the routes folder within the app
directory
• c) In the .env configuration file
• d) In the web.php or api.php files
within the routes directory
Which artisan command is used to
list all registered routes in a Laravel
application?

• a) php artisan routes:list


• b) php artisan route:list
• c) php artisan list:routes
• d) php artisan list
HTTP Methods

• GET, POST, PUT, PATCH, and


DELETE are the five most
common HTTP methods for
retrieving from and sending data
to a server.
The GET method

• The GET method is used to retrieve


data from the server. This is a read-
only method, so it has no risk of
mutating or corrupting the data.
For example, if we call the get
method on our API, we’ll get back a
list of all to-dos.
The POST method
The POST method sends data to the server
and creates a new resource. The resource
it creates is subordinate to some other
parent resource. When a new resource is
POSTed to the parent, the API service will
automatically associate the new resource
by assigning it an ID (new resource URI). In
short, this method is used to create a
new data entry
The PUT method
The PUT method is most often used to
update an existing resource. If you want
to update a specific resource you can call
the PUT method to that resource URI with
the request body containing the complete
new version of the resource you are trying
to update.
The PATCH method

The PATCH method is very similar to the PUT method


because it also modifies an existing resource. The
difference is that for the PUT method, the request body
contains the complete new version, whereas for the
PATCH method, the request body only needs to
contain the specific changes to the resource,
specifically a set of instructions describing how that
resource should be changed, and the API service will
create a new version according to that instruction.
The DELETE method

The DELETE method is used to


delete a resource specified by
its URI.
Route Parameters
Required Parameters - Sometimes you will need to
capture segments of the URI within your route. For
example, you may need to capture a user's ID from the
URL. You may do so by defining route parameters:

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


return 'User '.$id;
});
Route Parameters(contd.)
• You may define as many route parameters as required by
your route.

• Route parameters are always encased within {} braces


and should consist of alphabetic characters.

• Underscores (_) are also acceptable within route


parameter names.
Route Parameters(contd.)
Route::get('/posts/{post}/comments/{comment}', function
($postId, $commentId) {
//
});
Route Parameters(contd.)
Optional Parameters - Occasionally you may need to
specify a route parameter that may not always be present
in the URI. You may do so by placing a ? mark after the
parameter name. Make sure to give the route's
corresponding variable a default value:

Route::get('/user/{name?}', function ($name = null) {


return $name;
});

Route::get('/user/{name?}', function ($name = 'John') {


return $name;
});
View Routes
• If your route only needs to return a view, you may
use the Route::view method.

• This method provides a simple shortcut so that


you do not have to define a full route or controller.

• The view method accepts a URI as its first


argument and a view name as its second
argument.
View Routes(contd.)

Route::view('/welcome', 'welcome');
Route Parameters Constraints

You may constrain the format of


your route parameters using the
family of ‘where’ methods on a
route instance.
whereNumber Constraint:
The whereNumber constraint is used to ensure that a
route parameter is a numeric value.
It is useful when you want to restrict a parameter to only
accept numeric input.
=> And -> are different
=> (Arrow or Fat Arrow) -> (Arrow or Member Access
Operator)

• => is primarily used in • -> is used to access


association with key-value members (attributes or
pairs, often seen in data methods) of objects or
structures in some
structures like programming languages,
dictionaries, associative particularly in C-like
arrays, or objects in languages such as C++
various programming and PHP for object-
languages. oriented programming.
• It is commonly used to • It's used to access
assign values to keys or properties and methods of
properties. objects or structs.
whereAlpha
The whereAlpha constraint is used to ensure that a route
parameter contains only alphabetic characters.
It restricts the parameter to accept only letters (A-Z and a-
z).
whereAlphaNumeric
• The whereAlphaNumeric constraint is used to ensure
that a route parameter contains only alphanumeric
characters.
• It restricts the parameter to accept letters (A-Z and a-z)
and numbers (0-9).
whereIn
• The whereIn constraint is used when you want to
constrain a parameter's value to a specific set of values.
• It ensures that the parameter matches one of the
specified values.
where
• The where method is a more general constraint that
allows you to specify custom constraints using regular
expressions or closures.
• It provides flexibility to define complex validation rules
for a parameter.
Handling Multiple Parameter
Constraints

Suppose you have multiple parameters in URL, and you


want to have different constrains for both, then one
method for doing it is shown below.
Route Parameters Global Constraint
• Global Constraints - In Laravel, you can
define global route constraints by using the
pattern method

• This allows you to apply constraints to all


routes in your application. Global
constraints can be useful for enforcing
common validation rules on various route
parameters throughout your application.
• Here's how you can define a global route
constraint for all routes:

• Open your RouteServiceProvider located at


app/Providers/RouteServiceProvider.php.

• Inside the boot method of this class, use the


pattern method to define global constraints. For
example, let's say you want to enforce a
constraint that all route parameters named id
should be numeric:
• use Illuminate\Support\Facades\Route;

• public function boot()


• {
• parent::boot();

• Route::pattern('id', '[0-9]+');
• }
• In the above code, we're using the Route::pattern
method to specify a regular expression pattern [0-9]+ for
the id parameter. This pattern requires that any id
parameter in your routes must consist of numeric
characters.
Question 1: What is Laravel routing
primarily used for?

• A. Database management
• B. Handling HTTP requests
• C. Front-end development
• D. Server configuration
What is Laravel routing primarily
used for?

• A. Database management
• B. Handling HTTP requests
• C. Front-end development
• D. Server configuration
In Laravel, where are the routes
typically defined?

• A. In the .env file


• B. In the config/routes.php file
• C. In the routes/web.php and
routes/api.php files
• D. In the resources/views directory
In Laravel, where are the routes
typically defined?

• A. In the .env file


• B. In the config/routes.php file
• C. In the routes/web.php and
routes/api.php files
• D. In the resources/views directory
Which of the following is used to
pass parameters in a Laravel route?

• A. <param>
• B. [param]
• C. {param}
• D. (param)
Which of the following is used to
pass parameters in a Laravel route?

• A. <param>
• B. [param]
• C. {param}
• D. (param)
Which artisan command is used to list all
registered routes in a Laravel
application?

• A. php artisan serve


• B. php artisan list
• C. php artisan routes
• D. php artisan route:list
Which artisan command is used to list all
registered routes in a Laravel
application?

• A. php artisan serve


• B. php artisan list
• C. php artisan routes
• D. php artisan route:list
What is the primary purpose of
Laravel routing constraints?

• A. To define routes in the web.php file


• B. To restrict access to certain routes
• C. To specify rules that route parameters must
follow
• D. To create dynamic routes
What is the primary purpose of
Laravel routing constraints?

• A. To define routes in the web.php file


• B. To restrict access to certain routes
• C. To specify rules that route parameters
must follow
• D. To create dynamic routes
Which built-in constraint is used to
ensure that a route parameter is
numeric?

• A. whereNumber
• B. whereInteger
• C. whereNumeric
• D. whereDigit
Which built-in constraint is used to
ensure that a route parameter is
numeric?

• A. whereNumber
• B. whereInteger
• C. whereNumeric
• D. whereDigit
If you want to restrict a route parameter to
only alphabetic characters (A-Z and a-z),
which constraint would you use?

• A. whereAlpha
• B. whereAlphaNumeric
• C. whereRegex
• D. whereString
If you want to restrict a route parameter to
only alphabetic characters (A-Z and a-z),
which constraint would you use?

• A. whereAlpha
• B. whereAlphaNumeric
• C. whereRegex
• D. whereString

You might also like