05. Controllers - Laravel 11 tutorial for beginners
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
π 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.
π 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.
π 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
π‘Controllers
π‘PHP Artisan
π‘CRUD
π‘Validation
π‘Resource Controller
π‘Model
π‘Binding
π‘Route Resource
π‘Maintainability
π‘View
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
Hello friends Tony here welcome so in
the previous video we worked with the
routes so if I open
the web routes here we have what we have
done in the previous video we have the
route for the for slash when we navigate
the first page then we make a post
request a put delete and also to get a
test when we navigate the
SLS but in real world we don't use uh
anonymous functions because for example
here we make a post request for example
to create a new post or a user and when
we create a new user we are first going
to yeah here for example create new post
but before we creating a post also you
need to make up the logic to
validate validate the
data and then create new post and this
may be uh
12 or 20 lines the same thing for the
put request and the same thing for
delete for example and if if we have
more than just a post CR uh this file
may be uh too large for that we use
controllers and in laravel a controller
acts as the middleman between incoming
requests and the applications logic it's
a class that groups functions also
called uh methods and that handled this
request for instance a controller named
user controller might handle all
requests related to user such as
creating a new user uh updating a new
user deleting a new user and we use
controllers also for better organization
of your code by separating request and
logic from routes and this makes your
code more maintainable and easier to
understand and we have some commands to
create controllers let me open the
terminal and if I say PHP
Artisan list hit enter and here we have
all the lists what we can do with this
PHP Artisan so and we have a make
controller which is going to create a
new controller class okay so let's clean
and let's say
PHP artisan
make
colum controller and hit enter and we
have a prompt here what should the
controller be name and let's say for
example I'm going to make a post
controller now I'm going
to name this post controller you can
name post
plural but I prefer to use singular here
and also I want to add a controller at
the end because we're going to use so
with the models and I'm going to create
a post model so I don't have I don't
want to have a conflict with name so
let's specify this this is a post
controller and hit enter and now which
type of controller would you like we
have empty to be an empty class
resource this is going to have all the
functions or methods we need to make a
resource so a gr for post for example
then we have a single tone API and and
invocable so let's start with empty here
and as you can see now controller inside
and here is the path app HTTP
controllers post controller.php created
successfully let's see let's open the
directory up hasp controllers and here
we have the post
controller and we have an empty class
which extends the controller okay let's
delete this and because we are going to
make a crft for control for post and
let's create a new controller but
now let's name it
post controller again and let's choose a
resource in this case hit enter and what
model should this resource controller be
for this is optional so I'm going to
skip this okay now the controller
created let's close and here we have
post controller now if I open this as we
can see we have some methods here so the
index method and also here we have
display a listing of the resource for
example
post show the form for creating a new
resource here we have a create method a
store method store a newly created
resource in
storage in database then we have a show
to display the specific resource so
going to work to display a single post
edit show the form for editing the
specified
resource uh update to update the
specified resource in storage and then
we have a destroy now as we can see we
have here on the update also on the edit
and on the
show injected here string dollar sign ID
and we need that ID to find the post for
example and show display that specified
post but if I open and let's delete this
post controller just keep in mind this
in show we have string dollar sign ID
string dollar sign ID and also here
string doar sign string doar sign ID but
if I
delete and let's open the terminal let's
clean and let's run for the last time
the make controller let's let's make
post controller hit enter say resource
but now what model should this resource
controller before this is optional we
ski in the previous one now let's say
the model is post we don't have the
model but let's say for now model is
going to be post if I hit enter and we
have a prompt so the model does not
exist do you want to generate it yeah
and now we have generated also the model
for this post but what I wanted to show
you is if I open now the post
controller yeah we have the methods but
on the show now we don't have the string
dollar sign ID but we have the injected
the post model so is binding the post
directly not only the ID so we don't
need to find that post is directly pass
it here and also for the edit and update
and Destroy okay now let's use this
controller to clean
the route here okay so here we have the
post and this is going to be for example
for the SL post and instead of using
this Anonymous function we can use now
the controller we
created so inside the array we to add an
array inside the array we going to add
first the controller name so post
controller and make sure you import that
post controller up
here and col col
class and the second is going to be in
single quotes the name of the method so
for
post we want to create a new post and if
we go
here here we have the store which is
store a newly created resource in
storage so for post we're going to use
the store method and in single quot we
add the name of the method store here we
have a put which is going to be slash
post slash ID and instead of this
function also here we're going to use
the controller class and the name of the
method is going to be update then we
have a
delete and this is going to be a
controller class and the method the name
of the method is going to be destroy if
you see here we have the destroy
method then let's remove that here we
need to specify this is going to be
pause ID to
delete we have a post put and delete but
we don't have a get to get all the post
so let's create that route
get/ post and and we use the PO
controller class and then and the method
name is index then what we need is if I
also see here first we have the index
and we have the create we need a new
route for the create method so let's say
route get post create and using the post
controller class and the create
method and then we have a post then we
need to show a single post so let's
after we make a store we want to show a
single post so let's say get post okay
we use a get method here post pass in
here the ID because on the show method
we going to receive that post so larel
is smart enough instead of just passing
the ID and then find it here we are
going just bind the the model I'm going
to show you in the next video don't
worry so we have post then after the
show we have the edit so we need also
one more for the edit after
the show and here we have a get method
which is going to make a get request a
post passing also here the ID and then
the
edit we just going to return a view with
a form to edit the
post and now we are okay we have seven
routes and also on the post controller
we have seven control seven methods here
so the index one create je store three
show four edit five update six and
Destroy s okay now if I save this and
let's open the terminal I'm going to
clean and I'm going to use a pH P
artisan and instead of list I'm going
just specify I want a list of the routes
so route colon list hit
enter and here we have the first route
which is get then we have a post and get
the ignition ignore for now then we have
get puse as you can see here and then
here we have the controller with that
method okay post controller at index
then uh post post request post and for
that we have a post controller at store
and so on as you can see here we have
total 12 routes good but because we are
going to make a resource for the Post
instead of something like this we can
use just the route colon colon
resource and here we specify the URL
which is going to be
/st and here we just say in post
controller not specifying the methods or
something else just post controller and
that's it so let me comment this one
now and this is the same as what we have
added manually 1 by one here let's save
and let's open the terminal again okay
so here we have 12 routes if I clean and
run again that route list hit enter as
you can see again we have 12 routes but
now you can see on
the post uh post routes we also have the
names so you can see post Index this is
the name of the route okay so what do
mean if I uncomment this and let's
comment the rou resource here
if we want we can add the name for the
route and for example post index I'm
going to name this and let's open
Terminal and run again and as you can
see now
the get post has the post index name
because we added manually but instead of
doing all of them manually let's remove
and let's use this route resource okay
so now we have a clean web PHP file okay
and also here when we make a get request
slash and or use this Anonymous function
here we just have return a view now if
you want only the return a view because
in the real world we make a logic and
for that we use controllers but if you
want only to return a view instead of
this we can use a route colon colon view
okay and now here we specify the path in
this case it's going to be just slash
and here the name of the view which in
this case is going to be
welcome okay so this is the same as this
one but is more clean so let's remove
that and now we have very clean web PHP
file we created also the controller
which is going to be the logic for or
post
resource okay friends that's it all
about this video what I wanted to show
you now if you like such a videos don't
forget to subscribe to my channel like
the video share with your friends and
see you in the next one all the best
thank you very
much
Browse More Related Video
5.0 / 5 (0 votes)