Named Routes in Laravel




Named routes in Laravel allow you to assign a specific name to a route, making it easier to reference that route within your application. Instead of hardcoding URLs throughout your codebase, you can refer to routes by their assigned names.

Source Code

Laravel-routes\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
 
Route::get('/', function () {
    return view('welcome');
});
Route::get('/welcome-in', function () {
    return view('welcome');
})->name('welcome');
 
 
Route::get('/page1', function () {
    return view('page1');
});
 
Route::get('/page2', function () {
    return view('page2');
});
 
Route::get('/page3', function () {
    return view('page3');
});
 
Route::redirect("/welcome","/welcome-in",301);
 
Route::get('/sample', function () {
    return view('sample');
});
 
Route::get('/sample2', function () {
    return view('sample2');
});
Laravel-routes\views\welcome.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>Welcome</h1>

    <a href="/page1">page1</a>
    <a href="/page2">page2</a>
    <a href="/page3">page3</a>

    {{ 10+5 }}

    {{ "Tutor Joes" }}

    {{ "<h1>Welcome to Laravel</h1>" }}

    {!! "<h1>Welcome to Laravel</h1>" !!}

    {{-- {!! "<script> alert('working')</script>" !!} --}}

    @php
        $msg="Welcome to Laravel";
    @endphp
    {{ $msg }}
    @{{ $msg }}

    @php
        $i=0;
    @endphp
    @while ($i<6)
        <p>Sample Para {{ $i }}</p>
        @php
             $i++;
        @endphp
    @endwhile


    @php
        $users=["Ram","Sam","Raja","Kumar","Ravi"];
    @endphp
    
    <ul>
        @foreach ( $users as $user)
            <li>{{ $user }}</li>
        @endforeach
    </ul>

    <ul>
        @foreach ( $users as $user)
            <li>{{ $loop->index }} {{ $user }}</li>
        @endforeach
    </ul>

    <ul>
        @foreach ( $users as $user)
            <li>{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
        @endforeach
    </ul>

    <ul>
        @foreach ( $users as $user)
            <li>{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
        @endforeach
    </ul>

    <ul>
        @foreach ( $users as $user)
            @if($loop->first)
                <li style="color:green">{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
            @elseif($loop->last)
                <li style="color:red">{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
            @else
                <li>{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
            @endif
        @endforeach
    </ul>

    <ul>
        @foreach ( $users as $user)
            @if($loop->odd)
                <li style="color:green">{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
            @elseif($loop->even)
                <li style="color:red">{{ $loop->iteration }} / {{ $loop->count }} {{ $user }}</li>
            @endif
        @endforeach
    </ul>

    @php
        $products=[];
    @endphp

   <ul>
    @forelse ($products as $product)
        <li>$product</li>
    @empty
        <li>No Products Found</li>
    @endforelse
    </ul>
    <!--
        Template Directives 
        {{-- 
            @include
            @section
            @extend
            @yield
     --}}
    -->
    @php
        $details=[
            "name"=>"Ram Kumar",
            "age"=>25,
            "ismarried"=>"Yes"
        ];
    @endphp

    @include('pages.header',['data'=>'Sample Data','names'=>$users,'details'=> $details])
    @include('pages.footer')
    @includeWhen(true, 'pages.data', ['sample' => 'true data'])
    @includeUnless(false, 'pages.data', ['sample' => 'false data'])

     
</body>
</html>
Laravel-routes\views\page1.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Page-1</title>
</head>
<body>
  <h1>Page-1</h1>
  <a href="{{ route('welcome') }}">Welcome Page</a>

  <?php
    echo "Tutor Joes";
  ?>
</body>
</html>
Laravel-routes\views\page2.blade.php
<?php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Page-2</title>
</head>
<body>
  <h1>Page-2</h1>
  <a href="{{ route('welcome') }}">Welcome Page</a>
</body>
</html>
?>
Laravel-routes\views\page3.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Page-3</title>
</head>
<body>
  <h1>Page-3</h1>
  <a href="{{ route('welcome') }}">Welcome Page</a>
</body>
</html>
Laravel-routes\views\layouts\master.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>@yield('title','Page Title')</title>
  <link rel="stylesheet" href="{{ asset('css/app.css') }}">
</head>
<body>
  
  @yield('content')
  @yield('sample','No Sample Content')

  @hasSection ('sample')
    @yield('sample') 
  @else
    <h2>No Sample Content</h2>    
  @endif


  <aside>
   @section('sidebar')
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Product</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
   @show
  </aside>
</body>
</html>
Laravel-routes\views\sample.blade.php
@extends('layouts.master')
 
@section('title')
    Sample Joes Title
@endsection
 
@section('content')
    <h2>Sample Page</h2>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi hic distinctio, porro, eos consectetur laborum voluptatem tempora dolore maxime, minus incidunt? Minus quidem omnis odio, dolore nobis repellat necessitatibus blanditiis.</p>
@endsection
 
 @section('sidebar')
 @parent
 <p>sidebar Content</p>
@endsection 
Laravel-routes\views\sample2.blade.php
@extends('layouts.master')
 
@section('title')
    Sample Joes Title
@endsection
 
@section('content')
    <h2>Sample Page</h2>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi hic distinctio, porro, eos consectetur laborum voluptatem tempora dolore maxime, minus incidunt? Minus quidem omnis odio, dolore nobis repellat necessitatibus blanditiis.</p>
@endsection
 
{{-- @section('sample')
@endsection --}}
Laravel-routes\views\pages\header.blade.php
<?php
<h2>Header</h2>
{{ $data }}

<ul>
  @foreach ( $users as $user)
      <li>{{ $user }}</li>
  @endforeach
</ul>

<h5>Array with key</h5>
<ul>
  @foreach ( $details as $key=>$value)
      <li>{{ $key }} |  {{ $value }}</li>
  @endforeach
</ul>
?>
Laravel-routes\views\pages\data.blade.php
<p>Sample data by condition</p>
{{ $sample }}
Laravel-routes\views\pages\footer.blade.php
<h2>Footer</h2>