I did create a custom 404 page on my Laravel project by creating a folder called errors
and inside the folder I did create a file called 404.blade.php
However when I use a component and I try to pass the data with that component I get an error
<x-main-header-component></x-main-header-component>
</head>
<body>
<!-- Main Header -->
<x-main-navbar-component></x-main-navbar-component>
<!-- ... end User Menu Popup -->
error message here
<x-main-footer-component :message="$SocialMediaLinks"></x-main-footer-component>
</body>
</html>
the error that I'm getting is:
ErrorException
Undefined variable $SocialMediaLinks (View:
What can I do to pass the data to the component main-footer
on errors/404.blade.php
?
CodePudding user response:
There are more than one ways you can achieve this. Since we have no access to 404Controller
or anything like so, I usually come up with several ideas:
- Directly import the targetted data source (model for example). This will guarantee result, but the view may look ugly and not that logical.
- Caching: Redis, Memcache or anything will help. If your error views are using the same data source as your landing pages (navigation bar, header for example). The idea is that you will use
Cache::remember()
to store and get data whereever you want. Cache - View composer: (Docs). Cleanest way in my opinion. You just share data to all views (error views included) using global
*
regular expression. - Use helper: Since helpers are globally available, you can use it to store and retrieve data as you want.
For JavaScript framework component availability, just include it (the .js
file) in the error views under resources/views/errors
.
Example for the 3rd method:
In your AppServiceProvider@boot
:
view()->composer('*', function ($view) {
$view->with([
'somethingToShare' => "Hello world",
]);
});
In your error views:
{{dd($somethingToShare)}}
Now when you open your targetted error view, it should show "Hello World"
CodePudding user response:
I'm assuming you are calling $SocialMediaLinks in your app or footer. if you are, then use an helper function. i'll add my example below
@if (isset($seo))
<title>{{ $seo->title }}</title>
<meta name="description" content="{{ $seo->meta_description }}">
<meta name="keywords" content="{{ $seo->meta_keywords }}">
@else
@php
$seo = getSEO(); //helper function
@endphp
<title>{{ $seo->title }}</title>
<meta name="description" content="{{ $seo->meta_description }}">
<meta name="keywords" content="{{ $seo->meta_keywords }}">
@endif