'Laravel component constructor method is only working in local environment
My component looks like this:
namespace App\View\Components;
use Illuminate\View\Component;
use App\Http\Controllers;
use Illuminate\Http\Request;
class shopImageContainer extends Component
{
public $image;
public $ph;
public $va;
public $config;
/**
* Create a new component instance.
*@param string $ph
*@param string $va
* @return void
*/
public function __construct($ph, $va, $image)
{
$this->image = $image;
$this->va = $va;
$this->ph = $ph;
$this->config = Controllers\GlobalFunctions::getConfig($ph, $va);
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('components.shop-image-container');
}
}
in my component.blade I have this line/block that throws the error "Undefined variable: config":
@if($config->dd)
<div class="col bg-dark text-center p-3">
<a class="btn btn-primary rounded-0 " href="/download/{{ basename($image) }}" role="button">Download in {{ $config->ddLongEdge }}px</a>
</div>
@endif
It seems the constructor method is not being called (I tried dd("test) in the method and, there was no output) Interesting thing is, it's working in my local environment, but not on a server (apache2) I've read somewhere that there might be a bug with components and that there could be something wrong with the configuration of the server, in both cases there was no further detail on fixing the issue. Thanks for any help
Solution 1:[1]
I searched for a whole day trying to find a solution to this problem. My development environment was a Mac but my server was an Ubuntu environment.
It seems your filenames and classnames should follow the PSR-4 autoloading standards.
Here your filenames of the controllers and your controller names should be PascalCase and are case sensitive.
If the PSR-4 standard is not followed, your files are not auto loaded and this will trigger this error. As the construct method is not called while generating the view.
I solved this issue by renaming all my controllers and their names to PascalCase and to follow the PSR-4 standard.
This apparently fixed my issue.
You can run
composer update
There might be another command, but this will update your packages and also give a warning of the files which don't follow the PSR-4 standard.
By fixing these filenames and their controller names which will be shown in the warning to PSR-4 standard, it should solve your issue.
Solution 2:[2]
I encounter the same issue and I found the fix to be: (It is actually to follow PSR-4 standard)
shift the first letter of your component class to uppercase while naming it, like in my case I name it as:
Note that the component class name should also be capitalized as it has to hatch with the name of the file.
When you call a component it is up to you to choose uppercase or lowercase, from my experience I notice that it doesn't matter while calling :
<x-Header title="home" />
and <x-header title="home" />
works same
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dharman |
Solution 2 |