Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
89
Q:

html - How to use a php variable in all blades files in Laravel-5.4?

I am new to Laravel and currently working on a project where I need to use a PHP variable in all blade files. I'm using Laravel 5.4 version. I have tried a few methods, but none seem to work. Here's what I have tried so far:

1. I tried defining a global variable in the `AppServiceProvider.php` file, under the `boot()` method, like this:

```php
public function boot()
{
view()->share('globalVariable', $value);
}
```

Then, I tried accessing this variable in my blade file like `{{ $globalVariable }}`, but it didn't work.

2. I also tried creating a separate class file and then using a `@include` directive in my blade files to include that class file. But again, no success.

I'm wondering if there's any other way to achieve this. I would really appreciate any suggestions or solutions to solve this issue.

Thanks in advance!

All Replies

purdy.rusty

User 1:

Hey there! I had a similar issue, and after some research, I found a working solution. Instead of defining the variable in the `AppServiceProvider.php` file, try creating a middleware and passing the variable through it.

First, create a middleware by running the command `php artisan make:middleware GlobalVariableMiddleware`. Then, open the newly created middleware file located at `app/Http/Middleware/GlobalVariableMiddleware.php`.

In the `handle()` method of the middleware, you can define your global variable like this:

php
public function handle(Request $request, Closure $next)
{
$value = // your variable value here;

View::share('globalVariable', $value);

return $next($request);
}


Now, don't forget to register your middleware in the `app/Http/Kernel.php` file. Add the following line to the `$middleware` property or the `$middlewareGroups`'s `web` key:

php
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\GlobalVariableMiddleware::class,
],
];


After that, you can simply use `{{ $globalVariable }}` in any blade file, and the variable should be accessible.

I hope this helps you out! Let me know if you have any further questions.

martine.brakus

User 2:

Hello there! I faced a similar challenge when I started using Laravel 5.4. After trying out a few approaches, I found a solution that worked for me. Instead of using a middleware, I used the `View::composer()` method.

In your `AppServiceProvider.php` file, within the `boot()` method, add the following code:

php
public function boot()
{
View::composer('*', function ($view) {
$value = // your variable value here;
$view->with('globalVariable', $value);
});
}


By using `*` as the first parameter, the `composer` closure will be applied to all views. You can also specify specific views if needed, like `'home'` or `'partials.header'`.

Once you have added this code, you can simply use `{{ $globalVariable }}` in any blade file, and the variable will be available for use.

I hope this approach works for you as well. Let me know if you have any further questions or need more clarification.

New to LearnPHP.org Community?

Join the community