• <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 扩展表单类

        Laravel extend Form class(Laravel 扩展表单类)

        <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 扩展表单类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tfoot id='6rWPd'></tfoot>

                      <tbody id='6rWPd'></tbody>
                      <bdo id='6rWPd'></bdo><ul id='6rWPd'></ul>
                      <legend id='6rWPd'><style id='6rWPd'><dir id='6rWPd'><q id='6rWPd'></q></dir></style></legend>
                    • <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>