How Can I Create Basic Controllers In Laravel With Example

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored in the app/Http/Controllers directory.

How Can I Create Basic Controllers In Laravel With Example

Defining Controllers

1.Create demoController using below command

php artisan make:controller demoController


2.Open route\web.php and implement code 
Route::get('user_demo/{id}', 'demoController@show');

3.Open app\Http\demoController.php and implement code as below
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class demoController extends Controller
{
    public function show($id)
    {
        $user=User::findOrFail($id);
        return $user;
    }
}


Above example of a basic controller class. Note that the controller extends the base controller class included with Laravel. The base class provides a few convenience methods such as the middleware method.

when a request matches the specified route URI, the show method on the UserController class will be executed. The route parameters will also be passed to the method.

 

Controllers are not required to extend a base class. However, you will not have access to convenience features such as the middlewarevalidate, and dispatch methods.

 

Controllers & Namespaces

It is very important to note that we did not need to specify the full controller namespace when defining the controller route. Since the RouteServiceProvider loads your route files within a route group that contains the namespace, we only specified the portion of the class name that comes after the App\Http\Controllers portion of the namespace.

If you choose to nest your controllers deeper into the App\Http\Controllers directory, use the specific class name relative to the App\Http\Controllers root namespace. So, if your full controller class is App\Http\Controllers\Photos\AdminController, you should register routes to the controller like so:

Route::get('foo', 'Photos\AdminController@method');
 


Single Action Controllers

If you would like to define a controller that only handles a single action, you may place a single __invoke method on the controller:
 

1.Create controller using below command

php artisan make:controller ShowProfile --invokable


2.Create route and implement code

Route::get('user_demo/{id}', 'ShowProfile');


3.Open ShowProfile controller and implement code as below

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;

class ShowProfile extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return View
     */
    public function __invoke($id)
    {
        $user=User::findOrFail($id);
        return $user;
    }
}

 

Related Post