Laravel Controllers

Every controller class extends Laravel’s Base Controller class.

Basic Controller

We can code all functional elements, as well as communicate with the model in the controller and provide data to the view.

Route::get(“/”,[TestController::class,’index’]);

TestController class

TestController extends Controller{ function index(){ Return view(‘welcome’); }}

Single Action Controller

If we need to assign a specific controller to a route, we can use the PHP magic method invoke () in the controller. In that case, there is no need to include the method name alongside the controller in the route.

TestController extends Controller{

function __invoke(){

Return view(‘welcome’);

}}
Route::get(‘/’,TestController::class)

 

Generate invokable controller

php artisan make:controller ProvisionServer --invokable

Controller Middleware

In the controller, route middleware can be assigned.

Route::get(‘/’,[TestContoller::class,’index’)->middleware(‘testmiddleware’);

Resource Controller

If we create a controller using the below command it will automatically generate a few methods for us.

php artisan make:controller ReController –resource

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

 

When we declare a route using the resource method, it will generate all routes generated in the resource controller.

Route::resource('photos',[\App\Http\Controllers\ReController::class]);

Using the command below, we can check the route list.

php artisan route:list

ApiResource Routes

By using the ApiResource method we can automatically exclude the edit and delete methods from route.

Route::apiresource(‘’api’, ,[\App\Http\Controllers\ReController::class]);

We can also use —api with the command to create ApiResource controllers that lack edit and delete methods.

Php artisan make:controller ApiController –api

 

Nested Routes

Assume we have a user with a large number of posts. So if we want to get number 2 posts of user using URL we can get it like below.

www.laravelaura/users/1/posts/2

The user ID is 1 and the number of posts is 2.

So, in order to obtain this URL using a resource, we must define the route as shown below.

Route::resource('/ users. posts,\App\Http\Controllers\ResourceController::class);

And in Controller, we must add a new parameter.

public function show($id,$post)
{

    return "User Id:  : ".$id."User Post :".$photos;

}

Scoping Nested Resources

Laravel’s “Implicit model binding” automatically resolves nested bindings, which means that the child model must be dependent on the parent model or no result will be displayed.

 

Scoping Resource Routes:

Route::get('/posts/{post}/comments/{comment:test_id}', function (Post $post, Comment $comment) {
    return $comment;
});

If we have a route like the one above with a custom key test id, we can also define it using the resource like the one below. Then both routes will produce the same result. This is called scoping resource routes

Route::resource('posts.comments',CommentController::class)->scoped(
    ['comment'=>'test_id',]
);

 

 

CommentController::class 

 public function show(Post $post, Comment $comment)
{
    return $comment;
}

 

As a result, in nested bindings, the child model is bound to the parent model via Laravel’s scoped implicit model binding or by instructions provided by us. In the scoped method, we can define the key.

Leave a Comment

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

Scroll to Top