I have some common data getting from helper
. Now I wanted to use that data in all over view. So, I am trying to declare that data in app.blade.php
and trying to pass it's sections.
Here is my app.blade.php
-
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>ABC| @yield('title')</title>
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<link href="{{ asset('/assets/plugins/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ asset('/assets/abc/css/style.css') }}" rel="stylesheet">
<?php
//get theme
$theme = AppHelper::instance()->getTheme();
//get theme folder
$themeFolder = $theme[0]->websiteAdmin;
//set include files section path
$includePath = 'frontend.'.$theme[0]->themeName.'.sections.';
?>
</head>
@yield('content')
</html>
Here I want to pass variables $theme, $themefolder, $includePath
to @yield('content')
.
I tried below code -
@yield('content', array('theme'=> $theme, 'themeFolder'=> $themeFolder, 'includePath'=> $includePath))
But getting error that those variables are undefined.
Undefined variable: theme
Can you please help me how to pass data from @extend to @yield? Thank you in advance.
You should use View composers to accomplish this. They allow you to pass the same data across multiple views by only calling it in one place.
In your providers/AppServiceProvider.php
you can add the following to your boot method:
use Illuminate\Support\Facades\View; //import view facade
public function boot()
{
View::composer(['view-name', 'another-view-name'], function($view){
$theme = AppHelper::instance()->getTheme();
$themeFolder = $theme[0]->websiteAdmin;
$includePath = 'frontend.'.$theme[0]->themeName.'.sections.';
$view->with(compact('themeFolder', 'includePath', 'theme'));
});
}
View::composers
first argument takes an array of views, put all the views you want to pass the data to here ['view-name', 'another-view-name']
, it can also take a single string.
The data will now be available to your specified views through $themeFolder
$includePath
and $theme
variables
If you want this data to pass to ALL views, you can do '*'
as the first argument.
NOTE, this will pass the data to EVERY view you create, only do '*'
if you want every view to contain the data! Otherwise, specifiy the views individually.
If you want all the views within a certain folder to get passed the data you can do 'folder-name.*'
.
Alternatively, the only other way to pass data to your @yield
is to return the view app.blade.php
with the variables in your controller.