Laravel : How to call controller index function using routes

In this tutorial learn how to call controller from URL using route/web.php or call index function of controller from route, In Laravel its easy to create controller and view file but its understand to how to call controller from url, so, here routes/web.php files play the role between url and controller to access controller's method/function from url.

Let's go with the example using controller name is category

1. Create controller  in Controllers case sensitive name so, create like a example given example :CategoryController.php


//controller path<project_name>/app/Http/Controllers/<controller_name.php>

Example category controller




//controller path<project_name>/app/Http/Controllers/CategoryController.php

2. Create view  in views, create like a example :category


//view path<project_name>/resources/views/<view_name>.blade.php


Example category view


//view path<project_name>/resources/views/category.blade.php


3. Add Path to the routes file web.php

Edit web.php 


//route file name<project_name>/routes/web.php


Add this  data to the last 


//here Route::get('/<name_controller>', '<controller_name>@index');




Example category view explain to this how it work with the example

here, we can use any name in the url to call like http://127.0.0.1:8000/category or we can give name anyother like http://127.0.0.1:8000/categories   also, with replace name in route file also.


Route::get('/category', 'CategoryController@index');


4. Example category Add this code to the controller  use as you created your controller name here we created CategoryController.php
//controller path
<project_name>/app/Http/Controllers/CategoryController.php


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CategoryController extends Controller{
    /**     * Show the application dashboard.     *     * @return \Illuminate\Contracts\Support\Renderable     */    public function index()    {        return view('category');    }}







5. Add this code to view in Views  use as you created your view name here we created category.blade.php

//view path

<project_name>/resources/views/category.blade.php

@extends('layouts.app')
@section('content')<div class="container">    <div class="row justify-content-center">        <div class="col-md-8">            <div class="card">                <div class="card-header">Category</div>
                <div class="card-body">                    @if (session('status'))                        <div class="alert alert-success" role="alert">                            {{ session('status') }}                        </div>                    @endif
                    Welcome To the Category!                </div>            </div>        </div>    </div></div>@endsection


6. Add this code to web.php if you not added and if you given any other name of controller replace with it 

//route file name
<project_name>/routes/web.php

Route::get('/category', 'CategoryController@index');




7. Call from URL

//used php artisan servehttp://127.0.0.1:8000/category



//without php artisan servehttp://localhost/panel/public/category/ORhttp://localhost/panel/server.php/category





No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...