首页 > 解决方案 > Laravel 应用程序在本地运行良好,但在实时服务器上显示“未定义变量 $header”

问题描述

所以我有一个 Laravel 应用程序,在我的本地主机上它工作正常,没有问题。

然后,当我部署我的应用程序(我通过将 GitHub 存储库直接克隆到 Hostinger(共享主机)中进行部署)时,它显示了以下错误。

未定义变量 $header 截图

下面是 Intro.php 文件(我还注意到,当我运行 dd("random text"); 相同的错误仍然可见并且没有 dd() 输出)

<?php

namespace App\View\Components\headers\home;

use App\Models\ApiLink;
use Illuminate\Support\Facades\Http;
use Illuminate\View\Component;

class Intro extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {        
        return view('components.headers.home.intro', [
            'header' => ApiLink::link('translations', 'key=headline'),
            'about' => ApiLink::link('links', 'identifier=about')
        ]);
    }
}

在 intro.blade.php 中有以下代码:

<div class="h-screen w-full flex justify-center items-center relative overflow-hidden -mt-16" style="background: #f9fdfa;">
    <div class="-mt-80 lg:-mt-36 text-center relative z-10">
        <h1 class="font-light text-4xl sm:text-6xl" id="home-headline">
            {{ $header[0]['value'] }}
        </h1>

        <x-button 
            to="pages.about" 
            :text="$about[0]['page_name']"
            extraStyling="mt-5" />
    </div>

    <video class="z-0 absolute -bottom-32 left-1/2 transform -translate-x-1/2 hidden md:block" style="width: 1200px" id="flame-video" muted loop autoplay>
        <source src="{{ asset('videos/flame3.webmhd.webm') }}" type="video/webm">
        Your browser does not support the video tag.
    </video>

    {{-- <x-decoration.flame /> --}}

    <x-headers.home.video-container />
    
    <x-headers.home.showreel-bar />
</div>

同样,在我的本地主机上一切正常。在我部署 Web 应用程序后的服务器上,它会中断并显示屏幕截图中显示的错误。


找到解决方案

在进行了更多挖掘之后,我偶然发现了这个类似的问题,它解决了我的问题。命名约定是 PascalCase(适用于 app/View/Components 文件夹中的文件夹和文件)。

标签: phplaravel-8

解决方案


推荐阅读