【发布时间】:2018-01-30 03:25:19
【问题描述】:
我是 Laravel 服务提供商的新手,最近遇到了一个问题。我正在尝试从所有视图(全局)中的服务提供者访问一个变量。该变量采用 json 格式。 这是我的服务提供者
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ExternalVarServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application ExternalVarServiceProvider.
*
* @return void
*/
public function boot()
{
$url = "json-source-url";
$json = json_decode(file_get_contents($url), true);
$var = $json["array_index"];
//var should be global to all blade files, app.blade.php is main templ
View::share( 'app', 'var' );
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
感谢您的阅读,帕特 :)
【问题讨论】:
标签: php json global-variables laravel-blade