The complete Lavarel framework development tutorial

Powered by Blogger.

Saturday, 30 July 2016

HTTP Controllers- Laravel Controllers Tutorial



Let's have a recap of HTTP Routing Tutorial. What we were doing? We were not using the much logics, but all the logics used were defined in the routes.php file. The HTTP Controllers are helpful when you want to separate the request handling logics from the routes.php. In general all the logics of a web app are stored in HTTP Controllers.

Didn't getting? Don't worry, we will make it more clear by dividing HTTP Controllers in modules.

HTTP Controllers: Creating a controller


We can create a controller using php artisan command. Follow the simple steps.

  • Open command prompt.

  • Change directory to your laravel project root folder.

  • Run the command (Replace <controller-name> with any identifier you want).
    php artisan make:controller <controller-name>

The controller has been created. What next?

Now check out the app -> Http -> Controllers folder. You will find a new file called <controller-name> (The name you given while creating a controller). The file will contain some basic coding. We can also add as many codes as required.

Let's make it more clear. Here I am going to take a example and then I'll show what else can be done using HTTP Controllers.

  • First create a controller user using command.
    php artisan make:controller UserController

    This will create a new file in your app -> Http -> Controllers folder. The file will contain a class named UserController.
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use App\Http\Requests;
    
    class UserController extends Controller
    {
        //
    }

    We will create a new method inside the class UserController to show the profile of the user.
    class UserController extends Controller
    {
        //
        public function userProfile(){
         return view(userProfile);
        }
    }

  • We can call this method from routes.php.
    Route::get('profile', 'UserController@userProfile');

  • Create a new file called userProfile.blade.php inside resources -> views directory.
    <!DOCTYPE html>
    <html>
    <head>
     <title>Profile</title>
    </head>
    <body>
     <h2>Give a Title</h2>
     <p>Create a stunning user Profile page here.</p>
    </body>
    </html>

  • Visit localhost/<YourProjectFolder>/profile from your browser.

  • We can also pass argument to the route like this.
    Route::get('profile/{id}', 'UserController@userProfile');

    class UserController extends Controller
    {
        //
        public function userProfile($id){
         
        }
    }

HTTP Controllers: RESTful Resource Controllers


Laravel makes it very easy to perform CRUD (Create, Read, Update, Delete) operations on a resource stored in your app. You can create a resource controller that automatically provides all the methods for performing CRUD operations. Even, you don't need to register different routes for all the operations. You can register a single route to perform all the RestFul resource operations.

Creating a resource controller:

The method of creating a resource controller is same as normal controller, just at the end we need to put a suffix 'resource'.
php artisan make:controller FileController --resource

Now check your FileController.php file located at apps -> Http -> Controllers directory. The file will already have the basic functions.

Register a route:

We can register a single route for all the CRUD operations.
Route::resource('file', 'FileController');

This route will create multiple routes that can be used for different purpose like creating, reading, updating and deleting resources. Following are the different URI's that can be used on a resource.
Verb URI Action Route Name
GET /file index file.index
GET /file/create create file.create
POST /file store file.store
GET /file/{file} show file.show
GET /file/{file}/edit edit file.edit
PUT/PATCH /file/{file} update file.update
DELETE /file/{file} destroy file.destroy

HTML form can only make GET and POST request. For PUT and DELETE requests you need to add an extra hidden _method field to spoof these verbs.

For PUT request-
<input type="hidden" name="_method" value="PUT">

For DELETE request-
<input type="hidden" name="_method" value="DELETE">

Partial Resource Routes:

We can limit a resource controller route to handle actions by specifying a subset.
Route::resource('file', 'FileController', ['only' => [
    'index', 'show'
]]);

This resource route can only handle index and show route.
Route::resource('file', 'FileController', ['except' => [
    'create', 'store', 'update', 'destroy'
]]);

Similarly, this resource route can be used for create, store, update and destroy routes.

Conclusion


So, now you know the 'C' of MVC (Model-View-Controller). I've tried to make this tutorial as much easy as I can. If you are stuck anywhere regarding HTTP Controllers: Laravel Controllers Tutorial, you can comment below and leave the rest to me.

And one more thing, don't forget to share this to reach the Laravel HTTP Controllers tutorial to more audience.

0 comments:

Post a Comment