2015-03-19 10 views
34

के अंदर सभी पंजीकृत चर सूचीबद्ध करें मैं लैरवेल 5 का उपयोग कर रहा हूं। मैं जानना चाहता हूं कि दृश्य के अंदर एक दृश्य में सभी चर क्या हैं।लार्वा व्यू

के बाद से सभी चर मैंने सोचा कि मैं सामान्य पीएचपी समारोह इस्तेमाल कर सकते हैं दृश्य दायरे में हैं: get_defined_vars();http://php.net/manual/en/function.get-defined-vars.php

कुछ इस तरह:

// resources/view/home.blade.php 
    <html> 
    <body> 
     <?php print_r(get_defined_vars()); ?> 
    </body> 
    </html> 

लेकिन मैं अगर वहाँ एक बेहतर है जानना चाहते हैं जिस तरह से (View::getData() की तरह कुछ)

नोट: get_defined_vars() deosn't काम becausee यह बेकार चर के सैकड़ों रिटर्न (Laravel घटक)

यह एक टुकड़ा (आंशिक) print_r(get_defined_vars()) का उपयोग कर (मुझे लगता है कि यह अनंत प्रत्यावर्तन पाश में चला जाता है) है:

 Array 
(
    [__path] => C:\net\laravel\storage\framework\views/8e030a77b0bdbacc2c4182fc04420d1d 
    [__data] => Array 
     (
      [__env] => Illuminate\View\Factory Object 
       (
        [engines:protected] => Illuminate\View\Engines\EngineResolver Object 
         (
          [resolvers:protected] => Array 
           (
            [php] => Closure Object 
             (
              [this] => Illuminate\View\ViewServiceProvider Object 
               (
                [app:protected] => Illuminate\Foundation\Application Object 
                 (
                  [basePath:protected] => C:\net\laravel 
                  [hasBeenBootstrapped:protected] => 1 
                  [booted:protected] => 1 
                  [bootingCallbacks:protected] => Array 
                   (
                    [0] => Closure Object 
                     (
                      [static] => Array 
                       (
                        [instance] => Illuminate\Bus\BusServiceProvider Object 
                         (
                          [defer:protected] => 1 
                          [app:protected] => Illuminate\Foundation\Application Object 
*RECURSION* 
                         ) 

                       ) 

                      [this] => Illuminate\Foundation\Application Object 
*RECURSION* 
                     ) 

                    [1] => Closure Object 
                     (
                      [static] => Array 
                       (
                        [instance] => Illuminate\Translation\TranslationServiceProvider Object 
                         (
                          [defer:protected] => 1 
                          [app:protected] => Illuminate\Foundation\Application Object 
*RECURSION* 
                         ) 

                       ) 

                      [this] => Illuminate\Foundation\Application Object 
*RECURSION* 
                     ) 

                   ) 

                  [bootedCallbacks:protected] => Array 
                   (
                   ) 

                  [terminatingCallbacks:protected] => Array 
                   (
                   ) 

                  [serviceProviders:protected] => Array 
                   (
                    [0] => Illuminate\Events\EventServiceProvider Object 
                     (
                      [app:protected] => Illuminate\Foundation\Application Object 
*RECURSION* 
                      [defer:protected] => 
                     ) 
+1

उदाहरण को छोटा बनाएं। सभी को यह सब पढ़ने की उम्मीद मत करो। – gawi

+0

hi @gawi: वह उदाहरण सिर्फ print_r (get_defined_vars()) का परिणाम दिखाएं और मैंने केवल एक भाग लिया –

उत्तर

43

उपयोग dd सहायक:

{{ dd(get_defined_vars()) }} 

और अधिक पढ़ें: https://laravel.com/docs/5.4/helpers#method-dd

अद्यतन (thx, @JoeCoder): आप आगे कर "बेकार" चर पर कटौती कर सकते हैं:

{{ dd(get_defined_vars()['__data']) }} 
+0

यह काम नहीं करता है, यह फ़ंक्शन सैकड़ों बेकार चर लौटाता है। मैं छवि पोस्ट नहीं कर सकता (पर्याप्त प्रतिष्ठा नहीं) अन्यथा मैं –

+2

@llnk दिखा सकता हूं यदि आप सुझाव के अनुसार 'dd' का उपयोग करते हैं, तो वे" बेकार "चर ढह गए हैं और केवल एक मुट्ठी भर (शीर्ष स्तर चर के) को दिखाते हैं। डेटा देखने के लिए बहुत आसान तरीका (जैसा कि 'print_r' के विपरीत है। – JoeCoder

+0

@llnk ने अभी इसका परीक्षण किया है," सैकड़ों बेकार चर "नहीं हैं: http://i.imgur.com/B6ciHRS.png –

3

आप Laravel 5.1 जो अब कस्टम निर्देशों का ब्लेड का विस्तार करने के आप इस उपयोगी लग सकते अनुमति देता उपयोग कर रहे हैं। आपको AppServiceProvider में निर्देशों को इस example में पंजीकृत करने या अपने प्रदाता को बनाने की आवश्यकता है।

 /** 
    * Blade directive to dump template variables. Accepts single parameter 
    * but also could be invoked without parameters to dump all defined variables. 
    * It does not stop script execution. 
    * @example @d 
    * @example @d(auth()->user()) 
    */ 
    Blade::directive('d', function ($data) { 
     return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); ?>", 
      null !== $data ? $data : "get_defined_vars()['__data']" 
     ); 
    }); 

    /** 
    * Blade directive to dump template variables. Accepts single parameter 
    * but also could be invoked without parameters to dump all defined variables. 
    * It works similar to dd() function and does stop script execution. 
    * @example @dd 
    * @example @dd(auth()->user()) 
    */ 
    Blade::directive('dd', function ($data) { 
     return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); exit; ?>", 
      null !== $data ? $data : "get_defined_vars()['__data']" 
     ); 
    }); 
0

बेहतर पठनीयता और डिबगिंग उद्देश्यों के लिए, आप भी एक सहायक जो एक सरणी में उत्पादन बदल जाता है बना सकते हैं।

// as per comment from Braunson add this to custom helpers function in app\helpers.php and include it via composer. 

if (! function_exists('da')) { 
    /** 
    * Dump the passed variables to array and end the script. 
    * 
    * @param mixed 
    * @return void 
    */ 
    function da() 
    { 
     array_map(function ($x) { 
      dd($x->toArray()); 
     }, func_get_args()); 
    } 
} 
+1

पर sureferencing '__data' के साथ भी गेज आप कोर या विक्रेता फ़ाइलों को संपादित नहीं करते हैं, इसके बजाय 'ऐप \ helpers.php' में कस्टम हेल्पर्स फ़ंक्शन जोड़ें और इसे संगीतकार के माध्यम से शामिल करें। – Braunson

संबंधित मुद्दे