Beginner: create Laravel controllers

Hi, If you are a beginner and want to know how to create laravel controller so this post is very useful for you. in this blog I will explain how to create controller in Laravel.

Controller is the main key component in Laravel. it will handle all user request and return appropriate response according to request call. in controller have simple PHP class that contains methods action and it will handle user request. each method can take one or more parameters it will passed by router method when user make request call.

if you want to create simple controller so you can use below commands

php artisan make:controller AgentController

after run this command, it will create controller in “app\Http\Controllers” folder called AgentController.php

AgentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AgentController extends Controller
{
    //
}

once when create AgentController then we can add custom method for index, show etc.

you need to create route in this path routes/web.php .

Route::get('/agents', [App\Http\Controllers\AgentController::class, 'index'])->name('index');
AgentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AgentController extends Controller
{
    //
    public function index()
    {
    	$agents="listing data";
        return view('agents.index', compact('agents'));
    }
}

it need to create view file under resources\views\agents folder index.blade.php

index.blade.php
@extends('layouts.app')


@section('content')
<div class="container mt-3">
<div class="row ">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Agents Management</h2>
        </div>
        <div class="pull-right">
          
            
        </div>
    </div>
</div>


<div class="table-responsive">
               <?php echo $agents; ?>
            </div>

</div>

@endsection

one other way to create controller with default -resource option. below command we can use to create controller in laravel.

php artisan make:controller AgentuserController --resource

when I create controller with resource option then it will generate some default method like index, create, store, show, edit, update and destroy.

AgentuserController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

index : It is used to display a list of resources.
create : It is used to display the form of creating a new resource.
store : It is used to handle form submission for creating a new resource.
show : It is used to display a specific resource information with show method.
edit : It is used to display edit form for an existing resource.
update : It is used to handle the form submission for updating an edit method resource.
destroy : It is used to handle the deletion for an existing resource.

for this controller we can define routes in routes/web.php or routes/api.php file. below I mentioning how to create route for this type resource controller.

Route::resource('agentuser', App\Http\Controllers\AgentuserController::class);

you can expand knowledge and improve skills by learning from blog posts in Laravel section through online education in https://webdeveloperindia.in

Leave a Reply

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

23 − 16 =