Skip to content

Instantly share code, notes, and snippets.

@antoniputra
Created November 15, 2014 21:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save antoniputra/d24e08abc56e14e43223 to your computer and use it in GitHub Desktop.
Save antoniputra/d24e08abc56e14e43223 to your computer and use it in GitHub Desktop.
Macro for easily create link with method (POST, PUT, DELETE) - Laravel 4
<?php
/**
* Usage..
* Just save this code to your helpers file (eg: routes, filters, etc...)
*
* Then..
* {{ Form::link('Delete Item', 'DELETE', route('anything.page'), array('class' => 'btn btn-primary'), 'Are you sure ?') }}
**/
/**
* @param $text string
* @param $method string (http verb)
* @param $action string (is your route/url)
* @param $attr string (is your attributes like : class, id, etc...)
* @param $confirm_message string (need confirmation, before firing action ?)
**/
Form::macro('link', function($text, $method, $action, $attr = array(), $confirm_message=null)
{
// attribute for form
$formAttr = array('method' => $method, 'url' => $action, 'style' => 'display:inline-block;');
// append onSubmit
if($confirm_message) $formAttr = array_merge( $formAttr, array('onsubmit' => 'return confirm("'.$confirm_message.'");') );
$output = Form::open($formAttr);
$output .= '<button type="submit"';
// give attributes
if (!empty($attr) AND is_array($attr)) {
foreach ($attr as $key => $value) {
if ($key != 'icon') {
$output .= ' '.$key.'="'. $value .'" ';
}
}
}
$output .= '>';
if(isset($attr['icon'])) {
$output .= '<i class="'.$attr['icon'].'"></i> ';
}
$output .= $text .'</button>';
$output .= Form::close();
return $output;
});
@arryanggaputra
Copy link

Nice resource... thank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment