简体   繁体   English

Laravel 8 带面包屑添加动态页面标题

[英]Laravel 8 with breadcrumbs add dynamic page title

I'm using laravel-breadcrumbs package by diglactic with laravel 8 forked from davejamesmiller/laravel-breadcrumbs I'm trying to add dynamic page title based on this breadcrumbs package . I'm using laravel-breadcrumbs package by diglactic with laravel 8 forked from davejamesmiller/laravel-breadcrumbs I'm trying to add dynamic page title based on this breadcrumbs package .

what I mean by dynamic page title ?我所说的动态页面标题是什么意思? let's imagine that every page I have it contain a page title section like this:让我们想象一下,我拥有的每个页面都包含这样的页面标题部分

HTML: (Every Page it have this section below the navbar) HTML:(每个页面在导航栏下方都有此部分)

<section class="py-5 text-center container">
    <div class="row py-lg-5">
      <div class="col-lg-6 col-md-8 mx-auto">
        <h1 class="fw-bold mb-4">Page Title Here</h1> // Dynamic Page Title Here
      </div>
    </div>
</section>

You can use @yield to cahnge a section of your template dynamically.您可以使用@yield动态更改模板的一部分。
check the documentation for more info.查看文档以获取更多信息。

The @yield is use to dynamic title @yield 用于动态标题

here i am create a template.blade.php file and title.blade.php .我在这里创建一个template.blade.php文件和title.blade.php

the title.blade.php is extends template.blade.php file and title can dynamic is work perfectly. title.blade.php是扩展template.blade.php文件和标题可以动态工作完美。

template.blade.php模板.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@yield('title')</title>
</head>
<body>
    The title is dynamically
</body>
</html>

title.blade.php title.blade.php

@extends('template')
@section('title')
    My Title
@endsection

You can make your own breadcrumb.你可以自己做面包屑。 First you should generate blade component something like php artisan make:component breadcrumb and then look at the code, I use it anyway and it success首先,您应该生成类似于php artisan make:component breadcrumb之类的刀片组件,然后查看代码,无论如何我都使用它并且它成功

AppServiceProvider.php AppServiceProvider.php

public function boot()
    {
        // your prev code
        view()->share('currentPath', explode('/', substr_replace(request()->path(), '', 0, 3)));
    }

componenents/breadcrumb.blade.php组件/breadcrumb.blade.php

<nav>
    <ul class="breadcrumbs">
                    <li class="breadcrumb-item pl-0">
                        <a href="{{ route('home') }}">Home</a>
                    </li>
                    @foreach ($paths as $path)
                    <li class="breadcrumb-item pl-0 text-capitalize 
                    {{ $loop->last ? 'active' : '' }}">
                        <a href="{{ url($path) }}">
                            {{ str_replace('-', ' ', $path) }}
                        </a>
                    </li>
                    @endforeach
    </ul>
</nav>

And use it in any view like this并在这样的任何视图中使用它

<x-breadcrumb :paths="$currentPath" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM