Created
November 7, 2013 15:30
-
-
Save dasmikko/7356525 to your computer and use it in GitHub Desktop.
Laravel 4 API
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 | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register all of the routes for an application. | |
| It's a breeze. Simply tell Laravel the URIs it should respond to | |
| and give it the Closure to execute when that URI is requested. | |
| | |
*/ | |
// Route group for API versioning | |
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function() | |
{ | |
Route::resource('url', 'UrlController'); | |
Route::resource('url/delete', 'UrlController@destroy'); | |
Route::resource('url/update', 'UrlController@update'); | |
}); | |
Route::get('/authtest', array('before' => 'auth.basic', function() | |
{ | |
return View::make('hello'); | |
})); | |
Route::get('/{slug}', 'PageController@index'); | |
Route::get('/', 'HomeController@index'); |
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 | |
class UrlController extends BaseController { | |
/** | |
* Display a listing of the resource. | |
* | |
* @return Response | |
*/ | |
public function index() | |
{ | |
$urls = Url::where('user_id', Auth::user()->id)->get(); | |
if (!$urls) | |
{ | |
$error = "Found 0 URLS!"; | |
} | |
else | |
{ | |
$error = "Found URLS"; | |
} | |
return Response::json(array( | |
'error' => $error, | |
'urls' => $urls->toArray()), | |
200 | |
); | |
} | |
/** | |
* Show the form for creating a new resource. | |
* | |
* @return Response | |
*/ | |
public function create() | |
{ | |
// | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @return Response | |
*/ | |
public function store() | |
{ | |
$url = new Url; | |
$url->url = Request::get('url'); | |
$url->description = Request::get('description'); | |
$url->user_id = Auth::user()->id; | |
// Validation and Filtering is sorely needed!! | |
// Seriously, I'm a bad person for leaving that out. | |
$url->save(); | |
return Response::json(array( | |
'error' => false, | |
'urls' => $urls->toArray()), | |
200 | |
); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function show($id) | |
{ | |
// Make sure current user owns the requested resource | |
$url = Url::where('user_id', Auth::user()->id) | |
->where('id', $id) | |
->take(1) | |
->get(); | |
if (!$url->toArray()) | |
{ | |
return Response::json( | |
array( | |
'urls' => $url->toArray() | |
), | |
404 | |
); | |
} | |
else | |
{ | |
return Response::json( | |
array( | |
'urls' => $url->toArray() | |
), | |
200 | |
); | |
} | |
return Response::json(array( | |
'error' => $error, | |
'urls' => $url->toArray()), | |
200 | |
); | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function edit($id) | |
{ | |
// | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function update($id) | |
{ | |
$url = Url::where('user_id', Auth::user()->id)->find($id);; | |
$url->url = Request::get('url'); | |
// Validation and Filtering is sorely needed!! | |
// Seriously, I'm a bad person for leaving that out. | |
$url->save(); | |
return Response::json(array( | |
'error' => false, | |
'message' => 'url updated'), | |
200 | |
); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function destroy($id) | |
{ | |
$url = Url::where('user_id', Auth::user()->id)->find($id); | |
$url->delete(); | |
return Response::json(array( | |
'error' => false, | |
'message' => 'url deleted'), | |
200 | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment