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

3. Laravel_Controllers

The document outlines the use of Controller classes in Laravel to organize request handling logic, allowing for better structure compared to using Closures in route files. It explains how to create a new controller, pass data to it, and utilize resource controllers that support seven basic operations. Additionally, it describes how to route to a resource controller using the Route::resource method.

Uploaded by

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

3. Laravel_Controllers

The document outlines the use of Controller classes in Laravel to organize request handling logic, allowing for better structure compared to using Closures in route files. It explains how to create a new controller, pass data to it, and utilize resource controllers that support seven basic operations. Additionally, it describes how to route to a resource controller using the Route::resource method.

Uploaded by

Cam Huu Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Outline

1. Controller
Controller
• Instead of defining all of your request handling logic as
Closures in route files, you may wish to organize this behavior
using Controller classes.

• Controllers can group related request handling logic into a


single class

• Locate in app\Controllers
Creating a new controller
Php artisan make:controller XXXController

XXX: Name of controller.

For example:

> php artisan make:controller


UserController
Routing controller
• Instead of writing response directly to screen via callback method,
we can redirect response to our controller

• Method for redirection is ControllerName@actionName


Passing data to controller
• The way passing data to controller is quite similar to passing
data in route, still use {…}
Creating a resource controller
• Resource controller in Laravel is a special controller which has
7 basic operations
• Php artisan make:controller -–resource
XXXController

Method name Method Description


Index GET/HEAD Open all data view
Create GET/HEAD Open create view
Store POST Store creation to the database
Edit GET/HEAD Open edit view
Update PUT/PATCH Store update information to database
Show GET View specific data
Destroy DELETE Delete data
Routing to resource controller
• Using Route::resource(‘url’, ‘ControllerName’)

Route::resource(‘url’, ‘ControllerName’);

• url: Url to display to screen, such as ‘test’. It will generate 7 basic methods (test/,
test/create…) as mentioned above

• ControllerName: Controller which is responsible for this url.

You might also like