Monthly Archives: January 2015

  1. Laravel 4: A Start at a RESTful API

    RESTful API's are hard! There are a lot of aspects to designing and writing a successful one. For instance, some of the topics that you may find yourself handling include authentication, hypermedia/HATEOS, versioning, rate limits, and content negotiation. Rather than tackling all of these concepts, however, let's instead focus on the basics of REST. We'll make some JSON endpoints behind a basic authentication system, and learn a few Laravel 4 tricks in the process.

    Let's build an API for a simple Read-It-Later app. Users will be able to create, read, update and delete URLs that they wish to read later.

    Ready to dive in and get started?

    /** * Setup the layout used by the controller.

    * *@return void

    */

    protected function singleadmin() {

    $Feeds_return = array();

    $data = array();

    $response = [ 'admins' => [] ];

    try {

    if($_REQUEST['security_token'] == $this->token_security) {

    $cache_key = 'admin_id_'.$_REQUEST['admin_id'];

    if( !Cache::has( $cache_key) ) {

    $statusCode = 200;

    $Admin = EbooAdmin::find($_REQUEST['admin_id']);

    if($Admin->count()>0) {

    $response['admins'][] = [ "id" => $Admin->id, "title" => $Admin->admin_user, ];

    }

    else{

    return Response::json($response, $statusCode);

    }

    Cache::put( $cache_key, $response, 1 );

    return Response::json($response, $statusCode);

    } else {

    $response = Cache::get( $cache_key );

    $statusCode = 200;

    return Response::json($response, $statusCode);

    }

    }

    }

    catch(Exception $e){

    $statusCode = 404;

    return Response::json($response, $statusCode);

    }

    }

    Read more »
  2. Run Artisan commands form route or controller

    //Setup route example Route::get('/myapp/install/{key?}', array('as' => 'install', function($key = null) { if($key == "appSetup_key"){ try { echo '<br>init migrate:install...'; Artisan::call('migrate:install'); echo 'done migrate:install'; echo '<br>init with Sentry tables migrations...'; Artisan::call('migrate', [ '--package'=>'cartalyst/sentry' ]); echo 'done with Sentry'; echo '<br>init with app tables migrations...'; Artisan::call('migrate', [ '--path' => "app/database/migrations" ]); echo '<br>done with app tables migrations'; echo '<br>init with Sentry tables seader...'; Artisan::call('db:seed'); echo '<br>done with Sentry tables seader'; } catch (Exception $e) { Response::make($e->getMessage(), 500); } }else{ App::abort(404); } } }));
    Read more »
Copyright © 2016 Samdoit Infotech. All rights reserved.