Sunday, January 24, 2016

Using Illuminate/Html in Laravel 5.2


As you know, long time ago, Illuminate/Html was remove from laravel because it is not a core feature. Today I need to use Form for one of my projects and I am going to show how to include in Laravel 5.2. (I prefer not to use Form but you never know).

1) Add to composer.json

"illuminate/html": "5.*"

2) Update your project

composer update

3) Edit the file config/app.php and add to providers

Illuminate\Html\HtmlServiceProvider::class,

4) Edit config/app.php and add to aliases

        'Html'      => Illuminate\Html\HtmlFacade::class,
        'Form'      => Illuminate\Html\FormFacade::class,

5) Check it for example using the code:

{!! Form::open() !!}

{!! Form::text('name', @$name) !!}

{!! Form::password('password') !!}

{!! Form::submit('Send') !!}

{!! Form::close() !!}


If you receive the error:

[Symfony\Component\Debug\Exception\FatalErrorException]                   

  Call to undefined method Illuminate\Foundation\Application::bindShared() 

that means you need an additional step. Laravel 5.2 use singleton instead bindShared which is deprecated. You have to change that in your vendor HtmlServiceProvider as:

/**
* 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)
{
$form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());

return $form->setSessionStore($app['session.store']);
});
}


Hope it helps





1 comment: