PHP code example of earthling-interactive / laravel-jsonapi

1. Go to this page and download the library: Download earthling-interactive/laravel-jsonapi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

earthling-interactive / laravel-jsonapi example snippets


	Route::options('api/{model}/{id?}', 'ApiController@handleRequest');
	Route::any('api/{model}/{id?}', 'ApiController@handleRequest');
	Route::any('api/{model}/{id}/links/{relation}', 'ApiController@handleRequest');
    

		  namespace App\Http\Controllers;
		use EarthlingInteractive\JsonApi\Request as ApiRequest;
		use EarthlingInteractive\JsonApi\ErrorResponse as ApiErrorResponse;
		use EarthlingInteractive\JsonApi\Exception as ApiException;
		use Request;

		class ApiController extends Controller
		{
			public function handleRequest($modelName, $id = null, $relation = null)
			{
				/**
				 * Create handler name from model name
				 * @var string
				 */
				$handlerClass = 'App\\Handlers\\' . ucfirst($modelName) . 'Handler';

				if (class_exists($handlerClass)) {
					$url = Request::url();
					$method = Request::method();
					$ption which must be gracefully handled to give proper response
					try {
						$res = $handler->fulfillRequest();
					} catch (ApiException $e) {
						return $e->response();
					}
					
					return $res->toJsonResponse();
				}

				// If a handler class does not exist for requested model, it is not considered to be exposed in the API
				return new ApiErrorResponse(404, 404, 'Entity not found');
			}
		}
    

         namespace App\Handlers;
      
          use Symfony\Component\HttpFoundation\Response;
          use App\Models\User;
          
          use EarthlingInteractive\JsonApi\Exception as ApiException;
          use EarthlingInteractive\JsonApi\Request as ApiRequest;
          use EarthlingInteractive\JsonApi\Handler as ApiHandler;
          use Request;
          
          /**
           * Handles API requests for Users.
           */
          class UsersHandler extends ApiHandler
          {
              const ERROR_SCOPE = 1024;
              
              /**
               * Handles GET requests. 
               * @param EarthlingInteractive\JsonApi\Request $request
               * @return EarthlingInteractive\JsonApi\Model|Illuminate\Support\Collection|EarthlingInteractive\JsonApi\Response|Illuminate\Pagination\LengthAwarePaginator
               */
              public function handleGet(ApiRequest $request)
              {
                  //you can use the default GET functionality, or override with your own 
                  return $this->handleGetDefault($request, new User);
              }
              
              /**
               * Handles PATCH requests. 
               * @param EarthlingInteractive\JsonApi\Request $request
               * @return EarthlingInteractive\JsonApi\Model|Illuminate\Support\Collection|EarthlingInteractive\JsonApi\Response
               */
              public function handlePatch(ApiRequest $request)
              {
                  //you can use the default PATCH functionality, or override with your own
                  return $this->handlePatchDefault($request, new User);
              }
          }
    

	 namespace App\Models;

	use EarthlingInteractive\JsonApi\Model as ApiModel;
	
	class User extends ApiModel {
		
		public $exposedRelations = ['friends'];
	
		public function friends()
		{
		    return $this->hasMany('App\Models\Friend');
		}
	}