Created
November 15, 2014 21:55
-
-
Save antoniputra/d24e08abc56e14e43223 to your computer and use it in GitHub Desktop.
Macro for easily create link with method (POST, PUT, DELETE) - Laravel 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice resource... thank