Form macros are a really cool part of Laravel. They are described in the documentation, but that doesn't show you exactly how to use them in your application. So here is how I do it.
But how do we get this to load when needed? My solutions is to simply have it load on the start of the application. I do this by making a folder in my app directory called "misc" which just holds files for different things that I have to startup this way. Inside of that folder I make a file called "form_macros.php". To load this file on startup, we are going to require it within app/start/global.php. Simply add
Upon loading the page you should see an input field which as a label of "First Name", which if clicked will also put the cursor into the input field.
That is all there is to loading, creating, and using custom form macros. Now go forth and make maintaining forms a breeze by using custom macros.
Load macros on the start of the application
To register the macros if we follow the docs we simply need to do something likeForm::macro('myField', function()
{
return '<input type="awesome" >';
});
But how do we get this to load when needed? My solutions is to simply have it load on the start of the application. I do this by making a folder in my app directory called "misc" which just holds files for different things that I have to startup this way. Inside of that folder I make a file called "form_macros.php". To load this file on startup, we are going to require it within app/start/global.php. Simply add
require app_path() . 'misc/form_macros.php';
to the end of the global.php file. This will make sure that the form macros are loaded up on every run of the software.Create the macro
Here is the initial code for a quick custom macro:
<?php
Form::macro(
'myInput',
function ($name, $text, $placeholder = null, $type = 'text') {
$label = Form::label($name, $text, array('class' => 'control-label'));
$input = Form::input($type, $name, null, array('placeholder'=>$placeholder));
$item = <<<HTML
<div class="control-group">
$label
<div class="controls">
$input
</div>
</div>
HTML;
return $item;
}
);
This is going to create a macro which only requires two parameters. First the name which goes into the for attribute of the label and the name attribute of the input. Then it requires the text to be displayed for the label. That is it. You can optionally pass a placeholder for the input item in, along with a different type of input. Upon being called, this will return whatever markup you set in the $item.
Use the macro
Now that your macro file is being loaded and it has a macro in it, just drop into a pages view to test it real fast. For the example macro we will use the following:
{{ Form::myInput('first_name', 'First Name', 'John/Jane') }}
That is all there is to loading, creating, and using custom form macros. Now go forth and make maintaining forms a breeze by using custom macros.
Update!
This post applies to Laravel 4.x applications. The HTML component was removed by default in 5.0 and how you echo raw output has been modified as well. So do be aware that using this in 5.0 will require changes and (a bit) more work, but it is possible if you really wish to do so.