• <legend id='OcHp3'><style id='OcHp3'><dir id='OcHp3'><q id='OcHp3'></q></dir></style></legend>
  • <small id='OcHp3'></small><noframes id='OcHp3'>

      <bdo id='OcHp3'></bdo><ul id='OcHp3'></ul>

      <tfoot id='OcHp3'></tfoot>
        <i id='OcHp3'><tr id='OcHp3'><dt id='OcHp3'><q id='OcHp3'><span id='OcHp3'><b id='OcHp3'><form id='OcHp3'><ins id='OcHp3'></ins><ul id='OcHp3'></ul><sub id='OcHp3'></sub></form><legend id='OcHp3'></legend><bdo id='OcHp3'><pre id='OcHp3'><center id='OcHp3'></center></pre></bdo></b><th id='OcHp3'></th></span></q></dt></tr></i><div id='OcHp3'><tfoot id='OcHp3'></tfoot><dl id='OcHp3'><fieldset id='OcHp3'></fieldset></dl></div>

        Laravel 扩展表单类

        时间:2023-10-31

        <small id='ar8Zh'></small><noframes id='ar8Zh'>

        <tfoot id='ar8Zh'></tfoot>
        • <i id='ar8Zh'><tr id='ar8Zh'><dt id='ar8Zh'><q id='ar8Zh'><span id='ar8Zh'><b id='ar8Zh'><form id='ar8Zh'><ins id='ar8Zh'></ins><ul id='ar8Zh'></ul><sub id='ar8Zh'></sub></form><legend id='ar8Zh'></legend><bdo id='ar8Zh'><pre id='ar8Zh'><center id='ar8Zh'></center></pre></bdo></b><th id='ar8Zh'></th></span></q></dt></tr></i><div id='ar8Zh'><tfoot id='ar8Zh'></tfoot><dl id='ar8Zh'><fieldset id='ar8Zh'></fieldset></dl></div>

                • <bdo id='ar8Zh'></bdo><ul id='ar8Zh'></ul>
                • <legend id='ar8Zh'><style id='ar8Zh'><dir id='ar8Zh'><q id='ar8Zh'></q></dir></style></legend>
                    <tbody id='ar8Zh'></tbody>

                  本文介绍了Laravel 扩展表单类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试扩展 L4.1 中的 Form 类,但我似乎遗漏了一些东西.我的文件基于 API 命名为 FormBuilder.php 并保存在 app/libraries/extended/FormBuilder.php 中.

                  I'm trying to extend the Form class in L4.1 but I seem to be missing something. My file is named FormBuilder.php based on the API and is saved in app/libraries/extended/FormBuilder.php.

                  <?php namespace Extended;
                  
                  class FormBuilder extends IlluminateHtmlFormBuilder {
                  
                  /**
                   * Create a text input field.
                   *
                   * @param  string  $name
                   * @param  string  $value
                   * @param  array   $options
                   * @return string
                   */
                  public function text($name, $value = null, $options = array())
                  {
                          $options = $options + array('id'=>"field-{$name}");
                          return $this->input('text', $name, $value, $options);
                  }
                  
                  }
                  

                  这实际上是我第一次尝试在 Laravel 中扩展核心类.我似乎不知道如何正确扩展像这个 Form 类这样的核心类.

                  This is actually the first time I've tried extending a core class in Laravel. I can't seem to put my finger on how to properly extend core classes like this Form class.

                  我将 "app/libraries/extended" 添加到我的 composer.json 文件中并同时运行了 composer.phar updatecomposer.phardump-autoload 但它似乎仍然使用核心类而不是我的扩展类.我忘了做什么?

                  I added "app/libraries/extended" to my composer.json file and ran both composer.phar update and composer.phar dump-autoload but it still seemed to be using the core class instead of my extended one. What am I forgetting to do?

                  推荐答案

                  要扩展/交换一个 Laravel 核心类,你可以创建一个 Service Provider:

                  To extend/swap a Laravel core class, you can create a Service Provider:

                  文件:app/App/Libraries/Extensions/FormBuilder/FormBuilderServiceProvider.php

                  <?php namespace AppLibrariesExtensionsFormBuilder;
                  
                  use IlluminateSupportServiceProvider as  IlluminateServiceProvider;
                  use AppLibrariesExtensionsFormBuilderFormBuilder;
                  
                  class FormBuilderServiceProvider extends IlluminateServiceProvider {
                  
                      /**
                       * Indicates if loading of the provider is deferred.
                       *
                       * @var bool
                       */
                      protected $defer = true;
                  
                      /**
                       * Register the service provider.
                       *
                       * @return void
                       */
                      public function register()
                      {
                          $this->app->bindShared('formbuilder', function($app)
                          {
                              $form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());
                  
                              return $form->setSessionStore($app['session.store']);
                          });
                      }
                  
                      /**
                       * Get the services provided by the provider.
                       *
                       * @return array
                       */
                      public function provides()
                      {
                          return array('formbuilder');
                      }
                  
                  }
                  

                  为它创建一个外观:

                  文件:app/App/Libraries/Extensions/FormBuilder/FormBuilderFacade.php

                  <?php namespace AppLibrariesExtensionsFormBuilder;
                  
                  use IlluminateSupportFacadesFacade as IlluminateFacade;
                  
                  class FormBuilderFacade extends IlluminateFacade {
                  
                      /**
                       * Get the registered name of the component.
                       *
                       * @return string
                       */
                      protected static function getFacadeAccessor() { return 'formbuilder'; }
                  
                  }
                  

                  这将是您的命名空间服务类:

                  This would be your namespaced service class:

                  文件:app/App/Libraries/Extensions/FormBuilder/FormBuilder.php

                  <?php namespace AppLibrariesExtensionsFormBuilder;
                  
                  use IlluminateHtmlFormBuilder as IlluminateFormBuilder;
                  
                  class FormBuilder extends IlluminateFormBuilder {
                  
                      public function text($name, $value = null, $options = array())
                      {
                          $options = $options + array('id'=>"field-{$name}");
                  
                          return $this->input('text', $name, $value, $options);
                      }
                  
                  }
                  

                  打开 app/config/app.php 和你的 Service Provider 到列表

                  Open app/config/app.php and your Service Provider to the list

                  'AppLibrariesExtensionsFormBuilderFormBuilderServiceProvider',
                  

                  并用你的替换 Laravel 的表单别名

                  And replace Laravel's Form alias with yours

                      'Form'            => 'AppLibrariesExtensionsFormBuilderFormBuilderFacade',
                  

                  为了测试,我创建了一个这样的路由器:

                  To test I created a router like this:

                  Route::any('test', function() {
                  
                     return e(Form::text('first_name'));
                  
                  });
                  

                  它给了我这个结果:

                  <input id="field-first_name" name="first_name" type="text">
                  

                  这篇关于Laravel 扩展表单类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Laravel Eloquent 比较日期时间字段中的日期 下一篇:在 laravel 中上传 AJAX 文件

                  相关文章

                    <tfoot id='6rWPd'></tfoot>

                      <bdo id='6rWPd'></bdo><ul id='6rWPd'></ul>
                    <legend id='6rWPd'><style id='6rWPd'><dir id='6rWPd'><q id='6rWPd'></q></dir></style></legend>
                  1. <small id='6rWPd'></small><noframes id='6rWPd'>

                    <i id='6rWPd'><tr id='6rWPd'><dt id='6rWPd'><q id='6rWPd'><span id='6rWPd'><b id='6rWPd'><form id='6rWPd'><ins id='6rWPd'></ins><ul id='6rWPd'></ul><sub id='6rWPd'></sub></form><legend id='6rWPd'></legend><bdo id='6rWPd'><pre id='6rWPd'><center id='6rWPd'></center></pre></bdo></b><th id='6rWPd'></th></span></q></dt></tr></i><div id='6rWPd'><tfoot id='6rWPd'></tfoot><dl id='6rWPd'><fieldset id='6rWPd'></fieldset></dl></div>