Is it beneficial to add Laravel Collective HTML package into your Laravel project?

Introduction

Laravel Collective is a set of components that have been removed from Laravel’s core framework and are no longer maintained by Taylor Otwell. Instead there is group of volunteers who decided to maintain following once famous packages:

In this tutorial, we are going to examine laravelcollective/html, trying to verify its usefulness in your future projects. Let’s start then!

Assumptions

  • php and composer are installed
  • laravel command is available

Solution

Create Laravel project from scratch

As you probably know, Laravel’s base project comes with three packages

In order to add Laravel Collective Html package, update composer dependency as shown below

Please note that Laravel Collective Html is not standalone package, it requires five other illuminate packages such as

Typically, when you create new Laravel project, you actually pulling laravel/framework package which replaces all illuminate packages required by Laravel Collective Html.

As a result, you only see laravelcollective/html package being added to your composer.json.

If this part is clear, let’s move on to adding Laravel Collective Html provider to the providers array of config/app.php

If you enjoy Laravel Facades, don’t forget to add two class aliases to the aliases array of config/app.php

Installation part is over, so let’s try to explore a few features from Laravel Collective Html package.

Features

General Form creation

Laravel Collective Html Laravel

If you care about HTML naming conventions, please keep in mind that Laravel Collective Html produces form with uppercased method and accept-charset as shown below.

Sophisticated Form creation

Laravel Collective Html Laravel

Form Model Bindings

Having following user

passed to the Blade template, you can easily bind it to the form, using Form::model method, instead of Form::open.

However there are some caveats.

  • if you want to use Form::date, make sure your model formats Carbon object properly. In order to do it, you could use Eloquent Mutators or benefit from trait Collective\Html\Eloquent\FormAccessible
  • you must name your inputs exactly the same as properties in your Eloquent model. You will face the wall if you intend to construct the input names as arrays such as user[email], user[updatedAt].

If you wanted to achieve the same with plain Laravel, here is the snippet

Macros and Components

In order to create a Macro or Component, head over to your AppServiceProvider and inside boot method place the following

Additionally, Components use Blade templates, so don’t forget to create one in resources/views/components/form/text.blade.php with following snippet

Having Macro and Component ready, here is how this can be invoked inside your Blade templates

If you wanted to achieve something similar in pure Laravel, it should be pretty straightforward. Just create a custom class which:

  • will use trait Illuminate\Support\Traits\Macroable from Laravel Illuminate Support
  • will reuse the logic from trait  Collective\Html\Componentable from Laravel Collective Html

Other FormBuilder and HtmlBuilder conveniences

I went carefully through all the public methods from Collective\Html\FormBuilder and Collective\Html\HtmlBuilder classes, using the most complicated combination of parameters possible, as presented below.

 

Creating pure Laravel’s counterparts should be straightforward. If you are struggling though, please check an example github repository I’ve created.

URL generation with global helpers

Laravel Collective Laravel

It’s worth to know that Laravel’s route and action methods provide third parameter called absolute, which is not covered by the Laravel Collective counterparts. If you set it to false, you will get the URI without the domain (e.g: /?param=value or /home?param=value).

Summary

We have gone through long journey together, trying to discover hidden gems of laravelcollective/html package. Along the way we experienced some issues and shortcomings of it, so let’s try to enumerate them now

  • not every input type from HTML standards is covered (missing types: range, week, month)
  • model bindings cannot handle html name arrays (e.g users[name], users[description])
  • misleading and not comprehensive documentation (better to explore methods from FormBuilder and HtmlBuilder classes)
    • misleading information about Form::textarea which doesn’t extend Form::input .
    • Form::textarea uses not documented option like size=10×5
    • zero information about FormAccesible trait
    • no information about HTML facade and its conveniences
  • package tightly coupled to Laravel, but not adapted into Lumen (here is an explanation)
  • generating URLs via link_to* methods or HTML::link* look a bit redundant, comparing to Laravel’s route, action, url global functions.
  • link_to_action and link_to_route miss 3rd parameter absolute, which is used by Laravel’s route and action helpers
  • FormBuilder::selectRange, FormBuilder::selectYear and FormBuilder::selectMonth don’t use 5th parameter from FormBuilder::select
  • Inconsistent HTML naming conventions

If I missed anything relevant, please write your findings in the comments.

Sample Code

https://github.com/tekmi/blogging-laravelcollectivehtml

Personal Opinion

Although laravelcollective/html brings some conveniences, I prefer to stay away from it. It doesn’t bring much benefits, instead introduces unnecessary dependencies and a bunch of new methods which you need to get used to (sadly some of them are too rigid to be usable)

In the end, it’s just a thin wrapper on top of html, so why not to write pure HTML instead? There are existing IDE snippets, such as PHPStorm Bootstrap 3 Snippets which can provide similar functionality. Additionally, you may consider creating your own snippets in PHPStorm or any other IDE of your choice.

It’s paramount to point out again that laravelcollective packages are no longer developed and maintained by Taylor Otwell, creator of Laravel. As a result,  there will be always some discrepancies, issues or delays every time a new Laravel’s version is released.

Most interesting parts are either not flexible enough or can be reproduced with little effort, instead of pulling in the whole package. Form model bindings cannot withstand complex Eloquent model’s relations nor utilize html’s name arrays. Macros can be used without laravelcollective/html while Components could be extracted to single class.

Lastly laravelcollective/html is not portable, meaning it’s tightly coupled to Laravel,  Surprisingly it doesn’t even work with Lumen.


If you enjoyed this post, then make sure you subscribe to my Newsletter

One Reply to “Is it beneficial to add Laravel Collective HTML package into your Laravel project?”

  1. This same thing can be said about Bootstrap which is tightly coupled with jQuery. Why not write pure html and css? Because libraries are here to help you. Learning things is part of being web developer, that’s what I enjoy about this job. There’s always something new to learn which can either make or break a project.

Comments are closed.