Laravel

Laravel RESTful Resource Controllers & Constructors

January 30, 2018

author:

Laravel RESTful Resource Controllers & Constructors

While creating an application, we often need to perform CRUD (i.e. Create, Read, Update, Delete) operations. Laravel handles this requirement seamlessly with a single command. Creating a controller with that particular parameter. Here it is:
Run following artisan command in the console from the root directory of the project:

php artisan make:controller PostController --resource

# Resource Controller Setup

When we open app/Http/Controllers/PostController.php, we can notice the ready scaffolding:

# app/HttpControllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
         public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

# Route Setup

Next, let’s link this fresh controller with web.php No, don’t worry you don’t have to link each and every CRUD method. For resource controllers, we can register routes as:

# routes/web.php
...
Route::resource('blog', 'PostController');

Tada! Done. Yes, that’s it. We just created CRUD functional controller and route. If you have any doubts, just run following command in the console to check:

php artisan route:list

Do you see something like this?
RESTful Resource List

Parting Thoughts
We have a working CRUD structure; soon we will create a working CRUD example once we are done setting up the database and some other stuff. For now, have a good look at the route list that --resource flag provides and use PostController to test your doubts and learn.

Leave a comment

Your email address will not be published. Required fields are marked *