I have a weird issue where i'm setting a variable for a Laravel 7 Component to be displayed in the component. While it works in my development environment (MAMP on MacOS) it does not work when i load it to my shared hosting in HostGator. In HostGator it throws a 500 error complaining about "Undefined Variable: page_title".
Any ideas as to what could be happening here?
<!-- App/Components/Layout/ContentHeader.php -->
<?php
namespace App\View\Components\layout;
use Illuminate\Support\Facades\Request;
use Illuminate\View\Component;
class ContentHeader extends Component
{
/**
* @var string
*/
public $page_title;
/**
* @var array
*/
public $breadcrumbs = [];
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
$this->page_title = 'Some title';
$crumb = new \stdClass();
$crumb->label = 'Home';
$crumb->active = false;
$this->breadcrumbs[] = $crumb;
$crumb = new \stdClass();
$crumb->label = $this->page_title;
$crumb->active = true;
$this->breadcrumbs[] = $crumb;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
return view('components.layout.content-header');
}
}
@jeevamugunthan as noted in the description this does work in development but does not work in a shared hosting environment - leads me to believe it is something with a server setting or missing feature. Thanks for the input though.
@ksandar opcache does not seem to be enabled on my shared hosting - at least the function is not available.
@diegoarbelaez it seems there are a bug with new blade components on some environments (see this issue)
btw, the problem may be in the class constructor. Try change your ContentHeader.php - rename $page_title to $pageTitle and add it to constructor attributes like this
<?php
namespace App\View\Components\layout;
use Illuminate\Support\Facades\Request;
use Illuminate\View\Component;
class ContentHeader extends Component
{
/**
* @var string
*/
public $pageTitle;
/**
* @var array
*/
public $breadcrumbs = [];
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($pageTitle = 'Some title')
{
//
$this->pageTitle = $pageTitle;
$crumb = new \stdClass();
$crumb->label = 'Home';
$crumb->active = false;
$this->breadcrumbs[] = $crumb;
$crumb = new \stdClass();
$crumb->label = $this->page_title;
$crumb->active = true;
$this->breadcrumbs[] = $crumb;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
return view('components.layout.content-header');
}
}
Thanks @ksandar , it does work if i pass the variable via the component tag and does seem to be ignoring the constructor, almost seems as if the component constructor is not being called.
Thanks for the link - issue does sound similar i'll follow up with them as i'm already on the Laravel mentioned there - will provide updates once resolved.
I am having a similar problem only I am on laravel version 9.19, you can check this link for the code but it is basically the same problem and I have not been able to solve it, help please.