n Create Single Action Controllers in laravel 7 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

in this article, I'll show you how to create a controller with just one action, you can use __invoke() method and even create invokable controller.

to generate invokable controller you can use this command:

php artisan make:controller ShowProfile --invokable

 

Add route to this controller:

Route::get('post/{id}', 'ShowPost');

 

and in ShowPost.php file you can handle the incoming request in __invoke() method:

<?php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class ShowPost extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request, $id)
    {
        $post =  Post::findOrFail($id);
        return view('posts.show', ['post' => $post ]);
    }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook