0% found this document useful (0 votes)
10 views18 pages

Day2-Php Web Application Framework

Uploaded by

Jerrymie Luyahan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Day2-Php Web Application Framework

Uploaded by

Jerrymie Luyahan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PHP WEB

APPLICATION
FRAMEWORK:
LARAVEL
Day 2

Routing
Controllers
Routing
What is Routing?

•Definition: Maps HTTP requests (URLs) to application logic


(closure/controllers).
•Purpose: Determines how users interact with your app (e.g., /contact →
contact page).
Routing Files

•Location: routes/ directory.


• web.php: Handles web routes (HTML, sessions, CSRF
protection).
• api.php: Handles API routes (stateless, auto-prefixed with /api).
•Laravel 11 Update: Cleaner default files with minimal boilerplate.
Basic Route
Definitions

Syntax:

HTTP Verbs: get, post, put, patch, delete, any.


Redirecting Routes

Simple Redirect:

Named Route Redirect:


Routing Directly to a
View

Render a Blade template without a controller:

Parameters:
URL: /about
View File: resources/views/about.blade.php
Data (optional): Pass variables to the view (e.g., $title).
Route Parameters

Required Parameters:

Optional Parameters:

Constraints:
Named Routes

Assign a name to a route:


Route Groups

Apply middleware, prefixes, or namespaces to multiple


routes:
Controllers
Basic Controllers

⮚ Classes that handle HTTP request logic (separate from routes).


⮚ Stored in app/Http/Controllers.

Key Features:
• Methods: Actions like index(), store(), update().
• Request Handling: Access input data via Request $request.
• Response Return: Views, JSON, redirects, etc.
Single Action
Controller

Purpose:
• Handle a single action (e.g., contact form submission).
• Uses the __invoke() magic method.

Use Case: Simplify routes with one-off logic (no need for
multiple methods).
Controller Middleware

What is Middleware?
•Filters HTTP requests (e.g., auth, logging).

Assigning Middleware to Controllers:

Common Middleware:
auth, throttle, verified, role:admin.
Restful Resource
Controller

Automate CRUD (Create, Read, Update,


Delete) routes with a single line.

Options:
Limit actions: ->only(['index', 'show'])
Exclude actions: ->except(['destroy'])
Restful Resource Controller
This generates a controller In routes/web.php:
(app/Http/Controllers/PostController.php) with all
RESTful methods:
Thank you

You might also like