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

Laravel 04

Laravel Notes

Uploaded by

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

Laravel 04

Laravel Notes

Uploaded by

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

Laravel Controllers :-

=======================
1. Introduction to Laravel Controllers :-
------------------------------------------
1. MVC Architecture :-
--> User interacts with a view.
--> Views alerts controller of particular event.
--> Model alerts view that it has changed.
--> View grabs model data and updates itself.

2. Controllers :-
--> Controller acts as a directing traffic between views and Models.
--> It can group related request handling logic into a single class.
--> Controllers are defined in the "app/Http/Controllers" directory.

3. Understanding Defined Terms :-


a. namespace :-
--> The 'namespace' is used because it allows you to reuse function
names and classes across multiple portions of an application.

b. use :-
--> The 'use' is used ti import the class to the current file.

c. Command :-
php artisan make:controller <Controller_Name>
php artisan make:controller UserController

===================================================================================
===================
2. Routing Controllers in Laravel :-
------------------------------------
1. Routing Controllers :-
--> Routing controllers allow you to create controller classes
that have methods for handling requests.

Example :-
-----------
a. Controller :-
-----------------
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller


{
public function index()
{
return "Hello, User";
}
}

b. Web.php :-
--------------
use App\Http\Controllers\UserController;
Route::get('/user',[UserController::class,'index']);

===================================================================================
===================
3. Resource Controllers in Laravel :-
-------------------------------------
1. A Resource Controller is used to create a controller that handles all
of your appliation's http requests.

Syntax :-
php artisan make:controller <Controller_Name> --resource
php artisan make:controller TestController --resource

Example :-
----------
a. Resource Controller:-
----------------------
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller


{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
//
}
}

b. Web.php :-
--------------
use App\Http\Controllers\TestController;

Route::resource('/test',TestController::class);

c. Assigning Route :-
---------------------
1. Actions Handled by Resource Controllers :-
----------------------------------------------
Verb URI Action
Route Name
GET /photos
index photos.index
GET /photos/create create
photos.create
POST /photos store
photos.store
GET /photos/{photo} show
photos.show
GET /photos/{photo}/edit edit
photos.edit
PUT/PATCH /photos/{photo} update
photos.update
DELETE /photos/{photo}
destroy photos.destroy

d. Check all the Routes :-


php artisan route:list

2. Partial Resource Route :-


-----------------------------
1. When declaring a resouce route, you may specify a subset of actions
that the controller should handle instead of the full set of default
actions.

//Only Resource Route


a. Route::resource('/photo',PhotoController::class)->only(['index','show']);

// Excapt Resource Route


b. Route::resource('/photo',PhotoController::class)-
>excapt(['create','store','update','destroy']);

You might also like