Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

A conversation on laravel-html

Original – by Freek Van der Herten – 4 minute read

Hi, how are you? I'm fine thanks! How are you?

I saw you released another package last week. Yup yup, you mean laravel-html, right? It was actually coded up by my colleague Sebastian, who did an awesome job.

But why put out yet another html generator? Html generation is a solved problem, right? Yes, it is but...

I'm mean we already have laravelCollective/html... Yes, but...

Why not just type html, ... come on, do we really need a package for this? It's easy to just type html. Let me say first that if you're happy with your current solution, just stick with it. There's no right or wrong here. Because everybody already has their own favourite solution, html generation is a pretty divisive subject. I believe that's why html generation was kicked out of the Laravel core.

Stop rambling, just tell why you've created this. Well, like probably most fellow artisans we have been using the laravel collective package for quite some time and we were quite happy with it. Since html generation got kicked out of the Laravel core and has been put in the laravel collective package it has not evolved much. Take for instance this piece of code:

Form::email('element-name', null, ['class' => 'my-class']);

Seems like this is quite simple solution, but a few things bother me. That null parameter for instance when there is no value. And the fact that you need pass an array as the third parameter. In our package all html can be built up with a fluent interface. So you can do this:

html()->email('element-name')->class('my-class');

Yeah ok, that's certainly nice, but do you plan to code all of your html like this? No, certainly not. For simple things I prefer just writing the html. But when it comes to generating forms, I prefer using our package. Because when typing forms manually there's so much things to you need to take care of: the method field, the csrf token field etc... it's very error prone.

Here's how you can create a form with laravel-html:

html()->form('PUT', '/post')->open();
html()->email('email')->placeholder('Your e-mail address');
html()->submit('Process this');
html()->form()->close();

That'll output:

<form method="POST" action="/post">
    <input type="hidden" name="_method" id="_method" value="PUT">
    <input type="hidden" name="_token" id="_token" value="csrf_token_will_be_here">
    <input type="email" name="email" id="email" placeholder="Your e-mail address">
    <input type="submit" value="Process this">"
</form>

As you can see those hidden _method and _token fields were added automatically.

Cool! You can also use models or anything that implements ArrayAccess really to prefil the values of the form.

html()->model($user);

html()->input('name');

So if name is set on user the form element will receive it's value.

<input type="text" name="name" id="name" value="John">

This all sounds interesting, please continue. Well, if you want to know more, you really should really check out the documentation. It contains all you need to know. If you have a question about the package feel free to open up an issue at GitHub.

Is there anything more that we should know about the package? It's PHP 7.1 only.

Wut? Can you create a PHP 7.0 or PHP 5.6 version? Boring. Next question.

But why did you make it PHP 7.1 only? My projects do not use that version yet. We mainly create packages to use them in our own projects. All our new client projects use PHP 7.1, so it makes no sense for us to create packages for an older version of PHP.

But PHP 7.0 is still an active version... I really don't care. You're of course free to fork the package and maintain your own copy that's compatible with an older PHP version. Yay for open source.

Ok, I'll go check it out! Please do, like already mentioned the docs are here. And be sure to take a look at the other packages we've created previously.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "A conversation on laravel-html"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.