PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 26, 2022

[FIXED] Share a variable through out Laravel application

 January 26, 2022     laravel, laravel-5, php, service-provider     No comments   

Issue

I have a variable.

$key_test = 123456789;

I want to be able to access this 1 variable anywhere in my app even in config files, models, controllers, and views.

I've tried adding it in my boot():

<?php

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\VSE, App\CURL;
use View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        $key_test = 123456789;

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

I've tried to access it in one of my config file. I kept getting null.

<?php

dd($key_test); <---- null

return [ ... ]; 

How do I access that variable in my config file?

Did I do it right in my boot()?


Solution

You can use a helper method for config.

config(['key_test'=>123456789])

Then access it through the same way,

config('key_test')


Answered By - Ian
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing