Class 'Form' not found in Laravel 5?

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: 0701_laravel_form_01 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 0701_laravel_form_02 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: 0701_laravel_form_03 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.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month

Written by