05. Controllers - Laravel 11 tutorial for beginners

Tony Xhepa
23 Mar 202414:47

Summary

TLDRIn this instructional video, Tony introduces the concept of using controllers in Laravel to streamline and organize web application development. He demonstrates the process of creating a 'PostController' using Artisan commands, explaining the benefits of controllers for code maintainability and clarity. Tony also illustrates how to define routes for CRUD operations within a controller, transitioning from anonymous functions to a more structured approach. The video concludes with a clean routing setup and an overview of the controller methods, encouraging viewers to subscribe for more informative content.

Takeaways

  • πŸ˜€ The video discusses the use of routes and controllers in web development with Laravel.
  • πŸ”§ In the script, the presenter demonstrates how to clean up and organize code by using controllers instead of anonymous functions for handling requests like POST, PUT, and DELETE.
  • πŸ“‚ Controllers act as middlemen between incoming requests and application logic, grouping related methods for handling specific types of requests.
  • πŸ›  The 'make:controller' Artisan command in Laravel is used to generate new controller classes for organizing code better.
  • πŸ“ The presenter creates a 'PostController' to handle all post-related requests, such as creating, updating, and deleting posts.
  • πŸ“Œ The script explains the difference between various types of controllers like 'empty', 'resource', and 'invocable', with 'resource' being chosen for CRUD operations.
  • πŸ”„ The presenter shows how to bind models directly to controller methods, which simplifies the process of finding and displaying specific resources.
  • πŸ“ The 'route:list' Artisan command is used to display all defined routes in the application.
  • πŸš€ The 'route:resource' Artisan command is introduced as a shortcut to automatically create routes for a resourceful controller.
  • πŸ“‘ The script also touches on the use of route names for better route identification and maintenance.
  • 🌐 Lastly, the presenter mentions the use of 'route:view' for returning views directly without the need for a controller when the logic is simple.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is about using controllers in Laravel to manage web routes and requests more efficiently and maintainably.

  • Why are anonymous functions not recommended for handling requests in real-world applications?

    -Anonymous functions are not recommended because they can lead to large and unmanageable files when dealing with complex logic, such as validation and data manipulation, which are common in real-world applications.

  • What is a controller in Laravel?

    -In Laravel, a controller is a class that acts as a middleman between incoming requests and the application's logic, grouping functions (methods) that handle specific requests.

  • What command is used to create a new controller in Laravel?

    -The command 'php artisan make:controller [ControllerName]' is used to create a new controller in Laravel.

  • What are the different types of controllers that can be generated in Laravel?

    -The different types of controllers that can be generated in Laravel are 'empty', 'resource', 'api', and 'invocable'.

  • What does the 'resource' type controller include?

    -The 'resource' type controller includes methods for all the standard CRUD operations: index, create, store, show, edit, update, and destroy.

  • What is the purpose of using a model when creating a resource controller?

    -Using a model when creating a resource controller helps to automatically inject the model into the controller methods, simplifying the code and making it more efficient.

  • How can you assign a specific model to a resource controller during its creation?

    -You can assign a specific model to a resource controller by specifying the model's name after choosing the 'resource' type, for example, 'php artisan make:controller PostController --model=Post'.

  • What is the benefit of using the 'route:list' command in Laravel?

    -The 'route:list' command in Laravel is beneficial for listing all the registered routes in the application, which helps in understanding and debugging the routing system.

  • How can you simplify the routing definition for a resource in Laravel?

    -You can simplify the routing definition for a resource in Laravel by using the 'Route::resource' method, which automatically creates the necessary routes for the specified resource.

  • What is the purpose of the 'Route::view' method in Laravel?

    -The 'Route::view' method in Laravel is used to return a view directly in response to a request, which is useful for simple routes that do not require complex logic.

Outlines

00:00

πŸ“ Introduction to Laravel Controllers

The first paragraph introduces the concept of using controllers in Laravel to manage routing and application logic. It explains that instead of using anonymous functions for handling requests like POST, PUT, and DELETE, controllers serve as middlemen, organizing code into classes and methods that are easier to maintain and understand. The video demonstrates how to create a 'PostController' using the PHP Artisan command-line tool, choosing the 'resource' option to generate methods for CRUD operations. The importance of naming conventions and avoiding conflicts with model names is also highlighted.

05:04

πŸ›  Setting Up CRUD Operations with Controllers

This paragraph delves into the specifics of setting up CRUD (Create, Read, Update, Delete) operations using controllers in Laravel. It discusses the methods generated by the 'resource' controller option, such as 'index', 'create', 'store', 'show', 'edit', 'update', and 'destroy'. The paragraph also covers the process of binding models directly to controller methods, eliminating the need to manually find the model by ID. The video script guides the viewer through updating routes to use the 'PostController' methods instead of anonymous functions, enhancing the organization and maintainability of the code.

10:07

πŸ”„ Streamlining Routes with Resource Controllers

The final paragraph focuses on streamlining the routing process by using the 'Route::resource' method in Laravel. It demonstrates how to automatically generate routes for a resource by specifying the controller and the URL prefix, thereby reducing the need to manually define each route. The paragraph also touches on the use of route names for better route identification and the option to return views directly from routes using 'Route::view'. The video concludes with a clean and organized 'web.php' file, showcasing the efficiency of using resource controllers and routes in Laravel.

Mindmap

Keywords

πŸ’‘Routes

In the context of web development, routes refer to the paths or URLs that a user can access to perform certain actions. In the video, routes are used to handle different HTTP requests such as GET, POST, PUT, DELETE, which are essential for interacting with the web application. For example, the script mentions creating routes for 'GET /' to navigate to the first page and 'POST /' for creating a new post or user.

πŸ’‘Controllers

Controllers in the video are described as middlemen that handle incoming requests and interact with the application's logic. They are classes that group functions or methods to manage specific actions related to a resource. The script emphasizes the use of controllers for better organization and maintainability of code, such as a 'PostController' to handle all operations related to posts.

πŸ’‘PHP Artisan

PHP Artisan is a command-line interface tool for Laravel, a popular PHP framework. It is used for various development tasks, including creating controllers and models. In the script, PHP Artisan commands like 'php artisan make:controller' are used to generate new controller classes, demonstrating its utility in streamlining the development process.

πŸ’‘CRUD

CRUD stands for Create, Read, Update, and Delete, which are the basic operations performed on a database. The script discusses implementing CRUD functionality through controllers, emphasizing the importance of separating logic from routes to make the code more maintainable. For instance, the 'store' method in a controller is used to create a new resource, while 'update' and 'destroy' handle modifications and deletions respectively.

πŸ’‘Validation

Validation is the process of checking the correctness and completeness of data before it is processed or stored. In the video, the script mentions the need for validation logic before creating a new post or user, ensuring that the data meets certain criteria before being accepted by the system.

πŸ’‘Resource Controller

A resource controller in Laravel automatically provides methods for CRUD operations on a resource. The script explains that choosing 'resource' when creating a controller with PHP Artisan generates methods like 'index', 'create', 'store', 'show', 'edit', 'update', and 'destroy', which correspond to the standard actions for a resource.

πŸ’‘Model

In the context of the video, a model represents a single table in the database and is used to interact with the data. The script discusses the creation of a 'Post' model, which is associated with the 'PostController' to handle data operations for posts. Models are essential for organizing and structuring data in a web application.

πŸ’‘Binding

Binding in Laravel refers to automatically injecting dependencies into a route or controller method. The script mentions that when a model is specified during the creation of a resource controller, the ID parameter is not passed as a string but rather the model itself is bound and injected, simplifying the process of retrieving a specific resource.

πŸ’‘Route Resource

Route resource is a feature in Laravel that allows the creation of a set of routes for a resource with a single line of code. The script demonstrates using 'Route::resource' to automatically generate the necessary routes for a resource, such as '/posts', which simplifies the routing process and reduces redundancy.

πŸ’‘Maintainability

Maintainability refers to the ease with which a software system can be modified to correct faults, improve performance, or add functionality. The video emphasizes the use of controllers and models to separate concerns, which enhances maintainability by making the code easier to understand and modify.

πŸ’‘View

A view in Laravel is a template that contains the HTML and presentation logic of a web page. The script mentions returning a view from a route, which is a common practice for displaying content to the user. For example, using 'Route::view' to return a 'welcome' view simplifies the process of rendering a static page.

Highlights

Introduction to the use of routes in web development with Laravel.

Explanation of the limitations of using anonymous functions for handling requests.

Introduction to controllers as a solution for better organization and maintainability of code.

Demonstration of creating a controller using PHP Artisan command.

Explanation of the difference between various types of controllers: empty, resource, API, and invocable.

Creation of a 'PostController' with an empty class structure.

Discussion on the use of resource controllers for CRUD operations.

Illustration of binding models directly to controller methods for efficiency.

Explanation of how to assign controller methods to specific routes.

Introduction of the 'route:list' Artisan command to view all registered routes.

Demonstration of using the 'route:resource' command for quick route generation.

Explanation of route naming for easier route referencing.

Introduction to the 'route:view' command for returning views directly from routes.

Final summary of the video content and its practical applications.

Encouragement for viewers to subscribe, like, and share the video.

Transcripts

play00:00

Hello friends Tony here welcome so in

play00:02

the previous video we worked with the

play00:04

routes so if I open

play00:06

the web routes here we have what we have

play00:11

done in the previous video we have the

play00:13

route for the for slash when we navigate

play00:17

the first page then we make a post

play00:19

request a put delete and also to get a

play00:23

test when we navigate the

play00:25

SLS but in real world we don't use uh

play00:29

anonymous functions because for example

play00:32

here we make a post request for example

play00:34

to create a new post or a user and when

play00:39

we create a new user we are first going

play00:41

to yeah here for example create new post

play00:45

but before we creating a post also you

play00:47

need to make up the logic to

play00:52

validate validate the

play00:55

data and then create new post and this

play00:58

may be uh

play01:01

12 or 20 lines the same thing for the

play01:04

put request and the same thing for

play01:06

delete for example and if if we have

play01:09

more than just a post CR uh this file

play01:12

may be uh too large for that we use

play01:16

controllers and in laravel a controller

play01:19

acts as the middleman between incoming

play01:23

requests and the applications logic it's

play01:26

a class that groups functions also

play01:30

called uh methods and that handled this

play01:33

request for instance a controller named

play01:36

user controller might handle all

play01:39

requests related to user such as

play01:41

creating a new user uh updating a new

play01:45

user deleting a new user and we use

play01:48

controllers also for better organization

play01:51

of your code by separating request and

play01:54

logic from routes and this makes your

play01:57

code more maintainable and easier to

play02:00

understand and we have some commands to

play02:02

create controllers let me open the

play02:05

terminal and if I say PHP

play02:08

Artisan list hit enter and here we have

play02:13

all the lists what we can do with this

play02:16

PHP Artisan so and we have a make

play02:19

controller which is going to create a

play02:21

new controller class okay so let's clean

play02:25

and let's say

play02:27

PHP artisan

play02:30

make

play02:32

colum controller and hit enter and we

play02:36

have a prompt here what should the

play02:38

controller be name and let's say for

play02:40

example I'm going to make a post

play02:43

controller now I'm going

play02:45

to name this post controller you can

play02:49

name post

play02:51

plural but I prefer to use singular here

play02:55

and also I want to add a controller at

play02:57

the end because we're going to use so

play03:00

with the models and I'm going to create

play03:01

a post model so I don't have I don't

play03:04

want to have a conflict with name so

play03:07

let's specify this this is a post

play03:09

controller and hit enter and now which

play03:13

type of controller would you like we

play03:15

have empty to be an empty class

play03:19

resource this is going to have all the

play03:22

functions or methods we need to make a

play03:24

resource so a gr for post for example

play03:28

then we have a single tone API and and

play03:30

invocable so let's start with empty here

play03:33

and as you can see now controller inside

play03:36

and here is the path app HTTP

play03:38

controllers post controller.php created

play03:41

successfully let's see let's open the

play03:46

directory up hasp controllers and here

play03:48

we have the post

play03:50

controller and we have an empty class

play03:54

which extends the controller okay let's

play03:57

delete this and because we are going to

play03:59

make a crft for control for post and

play04:03

let's create a new controller but

play04:06

now let's name it

play04:10

post controller again and let's choose a

play04:13

resource in this case hit enter and what

play04:17

model should this resource controller be

play04:20

for this is optional so I'm going to

play04:24

skip this okay now the controller

play04:26

created let's close and here we have

play04:29

post controller now if I open this as we

play04:32

can see we have some methods here so the

play04:35

index method and also here we have

play04:37

display a listing of the resource for

play04:40

example

play04:42

post show the form for creating a new

play04:46

resource here we have a create method a

play04:49

store method store a newly created

play04:51

resource in

play04:53

storage in database then we have a show

play04:57

to display the specific resource so

play04:59

going to work to display a single post

play05:03

edit show the form for editing the

play05:06

specified

play05:08

resource uh update to update the

play05:11

specified resource in storage and then

play05:14

we have a destroy now as we can see we

play05:17

have here on the update also on the edit

play05:22

and on the

play05:23

show injected here string dollar sign ID

play05:27

and we need that ID to find the post for

play05:30

example and show display that specified

play05:33

post but if I open and let's delete this

play05:37

post controller just keep in mind this

play05:40

in show we have string dollar sign ID

play05:43

string dollar sign ID and also here

play05:45

string doar sign string doar sign ID but

play05:48

if I

play05:51

delete and let's open the terminal let's

play05:54

clean and let's run for the last time

play05:57

the make controller let's let's make

play06:02

post controller hit enter say resource

play06:06

but now what model should this resource

play06:08

controller before this is optional we

play06:11

ski in the previous one now let's say

play06:14

the model is post we don't have the

play06:16

model but let's say for now model is

play06:18

going to be post if I hit enter and we

play06:21

have a prompt so the model does not

play06:23

exist do you want to generate it yeah

play06:27

and now we have generated also the model

play06:30

for this post but what I wanted to show

play06:33

you is if I open now the post

play06:36

controller yeah we have the methods but

play06:39

on the show now we don't have the string

play06:42

dollar sign ID but we have the injected

play06:47

the post model so is binding the post

play06:50

directly not only the ID so we don't

play06:53

need to find that post is directly pass

play06:55

it here and also for the edit and update

play07:00

and Destroy okay now let's use this

play07:03

controller to clean

play07:07

the route here okay so here we have the

play07:10

post and this is going to be for example

play07:12

for the SL post and instead of using

play07:16

this Anonymous function we can use now

play07:19

the controller we

play07:21

created so inside the array we to add an

play07:24

array inside the array we going to add

play07:26

first the controller name so post

play07:32

controller and make sure you import that

play07:35

post controller up

play07:36

here and col col

play07:40

class and the second is going to be in

play07:44

single quotes the name of the method so

play07:47

for

play07:48

post we want to create a new post and if

play07:51

we go

play07:53

here here we have the store which is

play07:56

store a newly created resource in

play07:58

storage so for post we're going to use

play08:00

the store method and in single quot we

play08:04

add the name of the method store here we

play08:08

have a put which is going to be slash

play08:11

post slash ID and instead of this

play08:15

function also here we're going to use

play08:18

the controller class and the name of the

play08:21

method is going to be update then we

play08:24

have a

play08:27

delete and this is going to be a

play08:30

controller class and the method the name

play08:32

of the method is going to be destroy if

play08:35

you see here we have the destroy

play08:39

method then let's remove that here we

play08:43

need to specify this is going to be

play08:44

pause ID to

play08:47

delete we have a post put and delete but

play08:51

we don't have a get to get all the post

play08:54

so let's create that route

play08:57

get/ post and and we use the PO

play09:00

controller class and then and the method

play09:02

name is index then what we need is if I

play09:06

also see here first we have the index

play09:09

and we have the create we need a new

play09:13

route for the create method so let's say

play09:16

route get post create and using the post

play09:20

controller class and the create

play09:22

method and then we have a post then we

play09:27

need to show a single post so let's

play09:33

after we make a store we want to show a

play09:37

single post so let's say get post okay

play09:40

we use a get method here post pass in

play09:43

here the ID because on the show method

play09:48

we going to receive that post so larel

play09:52

is smart enough instead of just passing

play09:54

the ID and then find it here we are

play09:57

going just bind the the model I'm going

play10:01

to show you in the next video don't

play10:03

worry so we have post then after the

play10:07

show we have the edit so we need also

play10:10

one more for the edit after

play10:14

the show and here we have a get method

play10:17

which is going to make a get request a

play10:20

post passing also here the ID and then

play10:23

the

play10:25

edit we just going to return a view with

play10:28

a form to edit the

play10:32

post and now we are okay we have seven

play10:36

routes and also on the post controller

play10:39

we have seven control seven methods here

play10:43

so the index one create je store three

play10:46

show four edit five update six and

play10:50

Destroy s okay now if I save this and

play10:54

let's open the terminal I'm going to

play10:57

clean and I'm going to use a pH P

play11:00

artisan and instead of list I'm going

play11:02

just specify I want a list of the routes

play11:06

so route colon list hit

play11:09

enter and here we have the first route

play11:12

which is get then we have a post and get

play11:15

the ignition ignore for now then we have

play11:19

get puse as you can see here and then

play11:22

here we have the controller with that

play11:26

method okay post controller at index

play11:30

then uh post post request post and for

play11:35

that we have a post controller at store

play11:38

and so on as you can see here we have

play11:40

total 12 routes good but because we are

play11:44

going to make a resource for the Post

play11:47

instead of something like this we can

play11:51

use just the route colon colon

play11:58

resource and here we specify the URL

play12:01

which is going to be

play12:03

/st and here we just say in post

play12:06

controller not specifying the methods or

play12:09

something else just post controller and

play12:11

that's it so let me comment this one

play12:16

now and this is the same as what we have

play12:21

added manually 1 by one here let's save

play12:25

and let's open the terminal again okay

play12:27

so here we have 12 routes if I clean and

play12:31

run again that route list hit enter as

play12:34

you can see again we have 12 routes but

play12:37

now you can see on

play12:40

the post uh post routes we also have the

play12:45

names so you can see post Index this is

play12:49

the name of the route okay so what do

play12:52

mean if I uncomment this and let's

play12:55

comment the rou resource here

play12:59

if we want we can add the name for the

play13:03

route and for example post index I'm

play13:05

going to name this and let's open

play13:08

Terminal and run again and as you can

play13:10

see now

play13:11

the get post has the post index name

play13:15

because we added manually but instead of

play13:19

doing all of them manually let's remove

play13:23

and let's use this route resource okay

play13:27

so now we have a clean web PHP file okay

play13:32

and also here when we make a get request

play13:34

slash and or use this Anonymous function

play13:37

here we just have return a view now if

play13:40

you want only the return a view because

play13:42

in the real world we make a logic and

play13:45

for that we use controllers but if you

play13:47

want only to return a view instead of

play13:49

this we can use a route colon colon view

play13:56

okay and now here we specify the path in

play14:00

this case it's going to be just slash

play14:03

and here the name of the view which in

play14:07

this case is going to be

play14:09

welcome okay so this is the same as this

play14:14

one but is more clean so let's remove

play14:18

that and now we have very clean web PHP

play14:23

file we created also the controller

play14:25

which is going to be the logic for or

play14:29

post

play14:31

resource okay friends that's it all

play14:33

about this video what I wanted to show

play14:35

you now if you like such a videos don't

play14:37

forget to subscribe to my channel like

play14:39

the video share with your friends and

play14:40

see you in the next one all the best

play14:41

thank you very

play14:45

much

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
LaravelRoutingControllersWeb DevelopmentCRUD OperationsPHP ArtisanCode OrganizationAPI DevelopmentModel BindingResource Controller