使用Blade在模板中获取所有赋值变量


get all assigned variables in a template using Blade

正如我在标题中所说的,我想在使用Blade与laravel 5.1的模板中获得所有分配的变量

的例子:

控制器:

class Ctrl extends Controller
{
  public function index() {
    $title = "i'm a title";
    $name = "i'm a name";
    return view('myview', compact('title', 'name'));
  }
}

模板:myview.blade.php

@getVars()

服务提供商:

class getVarsServiceProvider extends ServiceProvider {
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::directive('getVars', function() {
        // MUST return array('title' => "i'm a title", "name" => "i'm a name");
        return "all vars"; // <---- 
    });
    }
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

调用get_defined_vars()会为您工作吗?