SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

How to Create Controller in Laravel

Create a simple controller

Laravel provides a very handy way to create a controller file, simply run the artisan command below:

php artisan make:controller PhotoController

You will get a simple controller at app/Http/Controllers/PhotoController.php. This controller will not have any methods defined and it is perfect if you only want a plain controller class:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PhotoController extends Controller
{
    
}

Create a single action controller

If a controller action is complex enough for you to create a dedicated controller, You can create a controller class with a single action.

To accomplish this, run the artisan command below:

php artisan make:controller PhotoController --invokable

You will get a single action controller with a __invoke method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PhotoController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        //
    }
}

Create a resource controller

If you think of each Eloquent model in your application as a "resource", it is typical to perform the same sets of actions against each resource in your application. In this case, you can create a resource controller.

To accomplish this, run the artisan command below:

php artisan make:controller PhotoController --resource

You will get a resourceful controller with corresponding methods:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PhotoController 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)
    {
        //
    }
}

The End

We have covered all the possible ways to create different controllers in Laravel. Hope you find this article useful.