Hi everyone, I have the following code that renders a banner view in my Laravel application. Depending on the current route, the banner displays different content. The code works fine on my local environment, but when I deployed the application to DigitalOcean, I'm getting an error that says the $title variable is undefined. I suspect that the view is being rendered before the component is initialized. How can I fix this issue?"
I hope this helps!
this is my class component
namespace App\View\Components\web\header;
use Illuminate\View\Component;
class banner extends Component
{
public $title;
public $description;
public $imgPath;
public $referenceId;
public $referenceClass;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
$this->init();
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.web.header.banner');
}
private function init(): void
{
$attributesToSet = $this->mapRoutes(request()->route()->uri);
$this->setAttributes($attributesToSet);
}
private function mapRoutes(string $currentRoute): array
{
$dinamycRoutes = [
'/' => [
'title' => "Acompañamiento personalizado en desarrollo de software",
'description' => "Ofrecemos asesoría y soporte especializado para garantizar el éxito de tu proyecto. Contáctanos y asegura la mejor experiencia en desarrollo de software.",
// 'imgPath' => "img/banner_image_1.svg",
'imgPath' => '',
'referenceId' => "home",
'referenceClass' => "home-banner",
],
'about-us' => [
'title' => "Transforma tu empresa con Gestiones Integrales",
'description' => "Gestiones Integrales, el camino hacia el éxito empresarial con soluciones personalizadas e integrales.",
'imgPath' => "img/banner_image_2.svg",
'referenceId' => "about-us",
'referenceClass' => "about-us-banner",
],
'services' => [
'title' => "Servicios",
'description' => "Servicios desc",
'imgPath' => "img/banner_image_1.svg",
'referenceId' => "services",
'referenceClass' => "services-banner",
],
'contact' => [
'title' => "Contacto",
'description' => "Contacto desc",
'imgPath' => "img/banner_image_2.svg",
'referenceId' => "contact",
'referenceClass' => "contact-banner",
],
];
return filled($dinamycRoutes)
? $dinamycRoutes[$currentRoute]
: null;
}
private function setAttributes(array $attributes): void
{
foreach ($attributes as $key => $attribute) {
$this->{$key} = $attribute;
}
}
}
and this is my blade componnent
<div class="container-fluid px-0">
<div class="page-banner {{ $referenceClass }}">
<div class="row align-items-center flex-wrap-reverse h-100">
<div class="col-md-6 py-5 wow fadeInLeft">
<h1 class="mb-4">
{{ $title }}
</h1>
<p class="text-lg text-withe mb-5">
{{ $description }}
</p>
{{-- <a href="#" class="btn btn-primary btn-split">Watch Video <div class="fab"><span --}} {{--
class="mai-play"></span></div></a> --}}
</div>
<div class="col-md-6 py-5 wow zoomIn">
<div class="img-fluid text-center">
<img src="{{ asset($imgPath) }}" alt="" width="550px">
</div>
</div>
</div>
<a href="#{{ $referenceId }}" class="btn-scroll" data-role="smoothscroll"><span
class="mai-arrow-down"></span></a>
</div>
</div>
</div>