You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中@extends与@yield在新blade文件不生效问题求助

Laravel Blade模板继承不生效问题

我在Laravel中使用@extends、@section和@yield时,主页面welcome.blade.php可正常运行,但新建的product-gel.blade.php文件无论放在view根目录还是produ子目录,使用@extends('layouts.frontend')定义@section('content')后均不生效。

父模板(resources/views/layouts/frontend.blade.php)代码

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="../resources/stylecss/style.css">
        @vite(['resources/css/app.css', 'resources/js/app.js'])
        <script></script>
    </head>
    <body>
        <div id="app">
            <section id="header">    
                <div class="main-pages-box">
                    <div class="main-pages">
                        <a href="../resources/main_page/customer-care.php" class="customer-care">CUSTOMER CARE</a>
                        @if (Route::has('login'))
                                @auth
                                    
                                <a> {{Auth::user()->name}} </a>
            
                                <a href="{{ Auth::logout() }}">LOG OUT</a>
            
                                @else
                                    <a href="{{ route('login') }}">LOGIN</a>
            
                                    @if (Route::has('register'))
                                        <a href="{{ route('register') }}">REGISTER</a>
                                    @endif
                                @endauth
                        @endif
                    </div>
                </div>
                <div class="main-bar">
                    <div class="daraz-logo-container">
                        <a href="../public/index.php"><img src="../resources/images/daraz_logo.png" alt="daraz_logo" class="daraz-logo"></a>
                    </div>
                    <div class="search-box-container">
                        <form onSubmit="searching();">
                            <input type="text" placeholder=" Search on Daraz" name="search" class="search-bar" id="text-to-search">   
                            <a onclick="searching();" class="search-btn"></a>
                            <a href="../resources/main_page/cart.php" class="cart-btn"><img src="../resources/images/cart-icon.png" alt=""></a>
                            <a href="../resources/main_page/downloadnow.php" class="downloadnow-icon"><img src="../resources/images/downloadnow-icon.png" alt=""></a>
                        </form>   
                    </div>
                </div>
            </section>
            <script src="../resources/scriptfiles/searching.js"></script>
            
            @yield('content')
            
            <section id="footer">
                <div class="customer-care-end">
                    <h1>Customer Care</h1>
                    <ul>
                        <li><a href="">Help Center</a></li>
                        <li><a href="">How to Buy</a></li>
                        <li><a href="">Corporate & Bulk Purchasing</a></li>
                        <li><a href="">Returns & Refunds</a></li>
                        <li><a href="">Daraz Shop</a></li>
                        <li><a href="">Contact Us</a></li>
                        <li><a href="">Purchase Protection</a></li>
                        <li><a href="">Daraz Pick up Points</a></li>
                        <li><a href="">Fulfulled By Daraz (FBD)</a></li>
                    </ul>
                </div>
                <div class="codenthx">
                    <div class="QRcode"></div>
                    <div class="appdl">
                        <div class="QRthx"></div>
                        <p>Happy Shopping</p>
                    </div>
            
                </div>
            </section>
        </div>
    </body>
</html>

子模板(product-gel.blade.php)代码

@extends('layouts.frontend')
@section('content')

<div>
    <h1>testing</h1>
</div>

@endsection

排查与解决步骤

  • 配置对应路由:Laravel不能直接访问blade文件,必须通过路由解析。在routes/web.php中添加路由:

    // 若文件在view根目录
    Route::get('/product-gel', function () {
        return view('product-gel');
    });
    
    // 若文件在produ子目录
    Route::get('/product-gel', function () {
        return view('produ.product-gel');
    });
    

    之后通过http://你的域名/product-gel访问页面。

  • 校验模板路径:确认父模板路径为resources/views/layouts/frontend.blade.php,文件名和目录名拼写正确(Linux环境区分大小写)。

  • 清除视图缓存:Laravel会缓存编译后的Blade模板,执行命令清除缓存:

    php artisan view:clear
    
  • 修正资源引用方式:父模板中用了相对路径加载资源,会导致子页面资源加载失败,改用Laravel辅助函数生成正确路径:

    • 自定义CSS:asset('resources/stylecss/style.css')
    • 图片:asset('resources/images/daraz_logo.png')
    • 页面跳转:不要直接指向PHP文件,统一用route()辅助函数(如登录链接的写法)。
  • 检查语法拼写:确认子模板@extends('layouts.frontend')无拼写错误,比如layout少写字母、frontend文件名不符等。

内容的提问来源于stack exchange,提问作者Granat HD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 15:35:51