Skip to main content
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