Pages

Laravel : How to call controller index function using routes

In Laravel  Let's go with the example using controller name is Product

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

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

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

2. Create view  in views create like a example product

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

//view path
<project_name>/resources/views/product.blade.php
3. Add Path to the routes file web.php

Edit web.php

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

Add this content to web.php

//here 
Route::get('/<name_controller>', '<controller_name>@index');
Example Product 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/product or we can give name anyother like http://127.0.0.1:8000/Products   also, with replace name in route file also.

Route::get('/product', 'ProductController@index'); 
4. Example Product Add this code to the controller  use as you created your controller name here we created ProductController.php

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

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

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

//view path
<project_name>/resources/views/product.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">Product</div>
                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif
                    Welcome To the Product!
                </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('/product', 'ProductController@index');
7. Call from URL

//used php artisan serve
http://127.0.0.1:8000/product  

//without php artisan serve
http://localhost/panel/public/product/
OR
http://localhost/panel/server.php/product





1 comment:

  1. Laravel Development Company, Get a free quote for laravel web development requirement from a leading Laravel Development Company. Discuss your web project with us to get large discounts...Contact us : +91-9806724185 or Contact@expresstechsoftwares.com

    ReplyDelete

Popular Posts