One of the biggest confusions for people switching from Laravel 4 to Laravel 5 was using the Form class. Basically, Form::open() and related stuff doesn’t work out of the box. This article contains a solution for this problem.
If you try to use Form::open() or any of the Form methods in a fresh Laravel 5 install, you would get something like this:
At first, you would think that something is wrong with namespaces or folders and you need to specify Form in another way. But the truth is that in Laravel 5 version libraries of Form and HTML are not included by default. Personally, I am against that decision, but Taylor did it for the case of separation – in his opinion, not every project needs that functionality, and the core should be the core. Oh well – who are we to judge. Here’s how you get it all back.
In essence, Form and HTML are now separate packages, and you treat them exactly as such: add to composer.json, run composer install/update, add providers and facades to app.php config file and you’re good to go. Now, step by step:
1. Add illuminate/html to composer.json file:
"require": { "php": ">=5.5.9", "laravel/framework": "5.1.*", "illuminate/html": "5.*" },
2. Run composer update
3. Open a config file config/app.php and add service providers and alias:
// ... 'providers' => [ // ... Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, Illuminate\Html\HtmlServiceProvider::class, // ... ], // ... 'aliases' => [ // ... 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, 'Form' => Illuminate\Html\FormFacade::class, ],
4. Let’s try it out – here’s a view:
{!! Form::open(['url' => '/']) !!} {!! Form::text('name', '', ['placeholder' => 'Name']) !!} {!! Form::submit('Register') !!} {!! Form::close() !!}
Visual result – a simple form:
The weirdest thing that not only Form is taken away from Laravel core, but also there’s nothing about it in documentation anymore – you will not find a section on Forms in the docs. So I hope this tip would be even more useful.
https://github.com/LaravelCollective/html extends and maintains forms and html builders for Laravel. I think you’ll find the community is consistently referring users to this package now. See also: http://laravelcollective.com/
Great thing Shawn, have to definitely check it out. And maybe write a review some time 🙂
Thank you very much. I was typing illuminate\html\HtmlServiceProvider::class, instead of Illuminate\Html\HtmlServiceProvider::class, and i was searching this same from very long. It worked finally.
How to solve this error “Class ‘Illuminate\Html\HtmlServiceProvider’ not found” ?
.
Add illuminate/html to your composer.json and then run composer update
thanks – searched ages for that one – laracasts doesn’t cover how to enter with the ::class element.
thanks, but mine doesn’t work; when i run php artisan serve i get this error:
In HtmlServiceProvider.php line 36:
Call to undefined method Illuminate\Foundation\Application::bindShared()