Skip to content

Instantly share code, notes, and snippets.

@arifsetyawan
Last active December 16, 2015 04:19
Show Gist options
  • Save arifsetyawan/5376182 to your computer and use it in GitHub Desktop.
Save arifsetyawan/5376182 to your computer and use it in GitHub Desktop.
The Bootstrap Tags Manager Server Side API (code in laravel)
<?php
/**
* This class is the extensible controller class in laravel
* @author arifsetyawan me@arifsetyawan.com
* @since 3
*/
class Api_Tags_Controller extends Admin_Controller{
/**
* This function is used to get all current saved tags in the database
* the related tables defines in Tag model.
* @return Response::json() containing json data format return
*/
public function get_list(){
$response = array();
$list = "";
$tags = Tag::get();
if($tags){
foreach ($tags as $tag) {
$list[] = array('tag' => $tag->name);
}
$response['tags'] = $list;
}
return Response::json($response);
}
/**
* This function is used to get request of typeAhead polling system that tags manager
* provide to check if the typed tags is exist on the database, Though the tagsmanager use
* pooling but it will request each character typed.
* @return Response::json() containing json data format return
*/
public function post_list(){
$response = array();
$tagname = Input::get('tagname');
$list = "";
$tags = Tag::where('name','like','%'.Input::get('tagname').'%')->get();
if($tags){
foreach ($tags as $tag) {
$list[] = array('tag' => $tag->name);
}
$response['tags'] = $list;
}
return Response::json($response);
}
/**
* This function provide the push of new tags given from tags manager. Listening to a POST based request
* and work with "AjaxPush:" attribute in tagsmanager
* @return (true) ? true : none;
*/
public function post_add(){
$tagname = Input::get('tag'); // the standard of the input param is "tag"
try{
$new_tag = Tag::create(array(
'name' => $tagname,
));
} catch(Exception $e){
// return nothing
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment