Git Product home page Git Product logo

rodels's Introduction

Rodels

Remote models for Laravel.

Getting Started

Follow these instructions to get Rodels up and running in your Laravel project.

Prerequisites

You need Composer installed on your machine, but since you're already using Laravel, I guess you can skip this step.

Installing

Begin by installing this package through Composer.

composer require daniesy/rodels

Laravel Users

For Laravel users, there is a service provider you can make use of to automatically register the necessary bindings.

Laravel 5.5+ users: this step may be skipped, as we can auto-register the package with the framework.

// config/app.php
'providers' => [
	'...',
	Daniesy\Rodels\RodelsServiceProvider::class
],

'aliases' => [
	'...',
	'Remote' => \Daniesy\Rodels\Facade\Remote::class,
],

When this provider is booted, you'll gain access to a helpful Remote facade, which you may use in your controllers.

public function index()
{
	$users = Remote::users()->list();
	return response()->json($users); 
}

In Laravel 5, of course add use Remote; to the top of your controller.

Defaults

If using Laravel, there are only two configuration options that you'll need to worry about. First, publish the default configuration

php artisan vendor:publish

// Or...

php artisan vendor:publish --provider="Daniesy\Rodels\RodelsServiceProvider"

This will add a new configuration file to: config/rodels.php.

<?php
return [  
/*  
 |-------------------------------------------------------------------------- 
 | The remote API host 
 |-------------------------------------------------------------------------- 
 | 
 | Set this value to the url of the remote API you want to connect to | 
*/  

  'host' => env('RODELS_HOST'),  
  
/*  
 |-------------------------------------------------------------------------- 
 | The HTTP client used to send HTTP requests 
 |-------------------------------------------------------------------------- 
 | 
 | This option defines the http client that rodels will be using to connect 
 | to the remote API. 
 | 
 | Supported: "curl" 
*/  

  'client' => 'curl',  
  
/*  
 |-------------------------------------------------------------------------- 
 | The authentication method 
 |-------------------------------------------------------------------------- 
 | 
 | You can set the authentication method that will be used when connecting 
 | to the API. 
 | 
 | Supported: "key" 
*/  

  'auth' => 'key',  
  
/*  
 |-------------------------------------------------------------------------- 
 | Authentication configuration 
 |-------------------------------------------------------------------------- 
 | 
 | Configure the key authentication method. 
 | 
*/  

  'key' => [  
  'name' => 'api-key',  
  'value' => env('RODELS_KEY')  
 ],
];

Using the library.

We add two artisan commands, one will create endpoints and the other one rodels.

Endpoints

The first step when using Rodels is to create an endpoint.

php artisan make:endpoint Users -r

This will create a new class in app\Endpoints- in this case Users.php. In this class you should define all methods related to your endpoint.

All endpoints names should be nouns in plural form

Using the -r or --rodel option, automatically creates a rodel for this endpoint.

<?php  
  
namespace App\Endpoints;  
  
  
use App\Rodels\User;  
use Daniesy\Rodels\Api\Components\Endpoint;  
use Daniesy\Rodels\Api\Components\RodelCollection;  
use Daniesy\Rodels\Api\Exceptions\InvalidModelException;  
  
class Users extends Endpoint  
{  
	/**  
	* @param array $params  
 	* @return RodelCollection  
	* @throws InvalidModelException  
	*/  
	public function list(array $params = []) : RodelCollection  
	{  
		$response = $this->authRequest()->get("users", $params);  
		return new RodelCollection($response, User::class);  
	}  
	
	/**  
	* @param string $name  
	* @return User  
	*/
	public function find(string $name) : User  
	{  
		$response = $this->authRequest()->get("users", compact('name'));  
		return new User($response);  
	}  
}

Rodels

For every endpoint, there should be a rodel created as well. Rodels are like Models, but instead of using them for databases, they're used for remote APIs.

Create a new rodel with this command:

php artisan make:rodel User

This will create a new rodel class in app\Rodels- in this case User.php.

All rodels names should be nouns in singular form.

rodels's People

Contributors

daniesy avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

windawake

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.