I am trying to setup Form Macros in 5.3, I have a service provider working:
/App/Providers/MacroServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Superscrews\Extensions\Html\HtmlBuilder;
use App\Superscrews\Extensions\Html\FormBuilder;
class MacroServiceProvider extends \Collective\Html\HtmlServiceProvider
{
/**
* Register the HTML builder instance.
*
* @return void
*/
protected function registerHtmlBuilder()
{
$this->app->singleton('html', function($app)
{
return new HtmlBuilder($app['url']);
});
}
/**
* Register the form builder instance.
*
* @return void
*/
protected function registerFormBuilder()
{
$this->app->singleton('form', function($app)
{
return new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());
});
}
}
And Html and Form macro files eg:
/app/Superscrews/Extensions/Html/FormBuilder.php
<?php
namespace App\Superscrews\Extensions\Html;
class FormBuilder extends \Collective\Html\HtmlServiceProvider
{
public function textField($name, $label = null, $value = null, $attributes = [])
{
$comp = '<div class="form-group">';
$comp .= '<label for="' . $name . '">' . $label . '</label>';
$comp .= '<input id="' . $name . '" class="form-control" placeholder="">';
$comp .= '</div>';
return $comp;
}
}
This works well except I seem to have stomped all over the normal Form:: macro behaviour as {!! Form::model(...) !!} no longer works giving the below error. I figure I have extended the HtmlServiceProvider and destroyed the existing, but can't figure out how to fix this - help!!:
Call to undefined method App\Superscrews\Extensions\Html\FormBuilder::model() (View: /vagrant/website/resources/views/admin/category/edit.blade.php)
Just setup Collective how it is supposed to be setup. Follow the install instructions. There is no extending classes or creating your own providers for this.
Then you can add your macros to any service provider for your application.
public function boot()
{
\Form::macro('textField', function ( ... ) {
..
});
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community