Introducing the Controller in Laravel

Tram Ho

Hello friends. Today I will introduce you about Controllers in Laravel and how to use Controllers in Laravel.

1. Introduction

Instead of defining all the logic of handling your request in the routes.php file, you may want to manage this by using Controller classes. Controllers can group related HTTP requests into the same class. Controllers are stored in app/Http/Controllers .

2. Basic controller

Define controller

Below is an example of controller in laravel. We should note that all extended controller classes should inherit the Controller class provided by Laravel.

You can define a route to an controller action as follows:

Now, when a request matches the URI address of the show method route on the UserController class, the route parameters will also be passed into the method.

Controller && Namespaces

You should note that we do not need to write down the full controller address when defining a route. We only need to define the controller name after the AppHttpController namespace, because the RouteServiceProvider loads the route files and the controllers in that route are specified after the AppHttpController namespace. If you want to include your controllers in a subdirectory after the AppHttpController namespace, you can simply call the name of that directory in the route as follows:

3. Controller Middleware

Middleware can be assigned to controllers in your routes as follows:

However, it is more convenient to define middleware from the controller’s contructor function. Using the middleware method from within your controller, you can easily assign middleware to the controller. You can even restrict the middleware to specific methods in the controller class:

4. Resource Controllers

Resource controllers make it easier to build RESTful controllers around resources. For example, you might want to create a controller that handles HTTP requests related to “photos” stored in your application. Using the Artisan make: controller statement, we can quickly create controllers:

The Artisan command will generate the controller file at app/Http/Controllers/PhotoController.php . The controller will include a method for the operation of the resource available.

Next, you may want to register a multi-resource route for the controller:

This unique routing declaration creates multiple routes to handle a variety of RESTful action types for the “photo” resource. Similarly, the generated controller will have some root methods available for each action, including a note telling you which URIs and which HTTP methods (POST, GET, PUT, PATCH, DELETE) they handle. physical.

You can register multiple resource controllers as follows:

Because HTML forms cannot add PUT , PATCH , DELETE methods, you will need to add the _method field to fake these actions from HTTP. Or use @method in the Laravel blade:

Partial resource routing

When declaring a route resource you can specify it to use a partial resource using only or except methods:

Name the resource routing

By default, all resource controller actions have a name; However, you can override those names by passing a string of names of your choice:

Name the parameter of the resource routing

By default, Route :: resource will create parameters for your resource routes based on the resource name. You can easily override each basic resource by passing parameters in the optional string. The parameters string should be an associative array of resource names and parameter names:

The above example will generate the following URIs for the show routing of the resource:

/user/{admin_user}

5. Dependency Injection & Controllers

Constructor Injection

Laravel’s service container is used to handle all Laravel controllers. As a result, you can “type-hint” whatever dependencies your controller needs into the controller’s constructor. The dependencies will be automatically handled and added in the controller’s “instance”:

Method Injection

In addition to constructor injection, you can also type-hint dependencies on controller methods. For example, let’s type-hint the IlluminateHttpRequest instance into one of our methods:

If your controller’s method is also expecting input from the routing parameter, then simply list the arguments of the route behind the other dependencies. For example, if your routing is defined as follows:

Route::put('user/{id}', ' [email protected] '); You can still type-hint IlluminateHttpRequest and access your route parameter id by defining your controller method as follows:

6. Route Caching

If your application only uses controller routes, then you can use the advanced part of Laravel’s routing cache. Using routing caching will reduce the network time required to register all routes in your application. In some cases, your routing registration can be up to 100 times faster! To create routing memory, simply run the Artisan route: cache command:

php artisan route:cache That’s all! Your routing cache file will be used instead of the app / Http / routes.php file. Remember, if you add any new routes, you need to create a new route cache. Therefore, you should only run the route: cache statement during project development.

To delete the route cache file without creating a new file, use the route: clear command:

php artisan route:clear

So I introduced the basic controller in Laravel. Reference: https://laravel.com/docs/5.8/controllers#controllers-and-namespaces

Share the news now

Source : Viblo