久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      關(guān)于laravel自定義模板指令-tojs

      下面由Laravel教程欄目給大家介紹laravel自定義模板指令-tojs ,希望對需要的朋友有所幫助!

      Blade 允許你自定義命令,你可以使用 directive 方法注冊命令。當(dāng) Blade 編譯器遇到該命令時,它將會帶參數(shù)調(diào)用提供的回調(diào)函數(shù)。blade模板可以通過directive方法來自定義模板指定,

      tojs指令主要用于PHP自定義一些數(shù)據(jù)轉(zhuǎn)換為js對象方便js調(diào)用

      1.創(chuàng)建ToJsServiceProvider

      <?php  namespace AppProviders;  use AppHelpersToJsToJs; use IlluminateSupportFacadesBlade; use IlluminateSupportServiceProvider;  class ToJsServiceProvider extends ServiceProvider {     /**      * Bootstrap the application services.      *      * @return void      */     public function boot()     {         //     }      /**      * Register the application services.      *      * @return void      */     public function register()     {         $this->app->singleton('tojs', function () {             return new ToJs();         });          /*         * The block of code inside this directive indicates         * the chosen javascript variables.         */         Blade::directive('tojs', function () {             return '<script> window.Laravel = ' . json_encode(app('tojs')->get()) . '</script>';         });     } }

      2. ToJs方法主要是對數(shù)組的一些操作

      <?php  namespace AppHelpersToJs;  use IlluminateSupportArr;  class ToJs {     protected $data = [];      public function put(array $data)     {         foreach ($data as $key => $value) {             $this->data[$key] = value($value);         }          return $this;     }      public function get($key = null, $default = null)     {         if (!$key) return $this->data;          return Arr::get($this->data, $key, $default);     }      public function forget($keys)     {         Arr::forget($this->data, $keys);          return $this;     } }

      3.聲明facade

      namespace AppHelpersToJsFacades;  use IlluminateSupportFacadesFacade;   class ToJsFacade extends Facade {     /**      * Get the registered name of the component.      *      * @return string      */     protected static function getFacadeAccessor()     {         return 'tojs';     } }

      4.在config數(shù)組添加serviceProvider

      providers 添加
      AppProvidersToJsServiceProvider::class

      aliases 添加
      'ToJs' => AppHelpersToJsFacadesToJsFacade::class,

      5.為了方便調(diào)用可以在寫一個helper方法

      if (!function_exists('to_js')) {     /**      * Access the javascript helper.      */     function to_js($key = null, $default = null)     {         if (is_null($key)) {             return app('tojs');         }          if (is_array($key)) {             return app('tojs')->put($key);         }          return app('tojs')->get($key, $default);     } }

      在PHP代碼需要的地方調(diào)用 to_js(['username'=>'test']);

      blade模板直接通過 @tojs 就可以在頁面渲染出
      <script> window.Laravel = {"username":"test"}</script>

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號