2017 © Pedro Peláez
 

library repositories

Central repositories passing and receiving data between controller and model

image

arch/repositories

Central repositories passing and receiving data between controller and model

  • Wednesday, March 15, 2017
  • by metallurgical
  • Repository
  • 2 Watchers
  • 2 Stars
  • 33 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

Arch Repositories

Laravel Package for Central repositories passing and receiving data from database. This package react as a bridge between Controller, Model and database. All the reusable code should located in one place and called when needed., (*1)

Package Installation

1) Package can be installed using composer, just require it :, (*2)

`composer require arch/repositories`

2) After install, add ArchServiceProvider to provider array in config/app.php as follow :, (*3)

'providers' => [
  ............
  Arch\Repositories\ServiceProvider\ArchServiceProvider::class
];

Canal, Repositories and Model Installation

Before everything can be used, we must create Canal and Repositories for the application and make use of trait class for the Model., (*4)

1) Generate Repositories

Repositories is a normal php class having all the functionalities to play with the Laravel Query Builder for database querying. You can find out all the available method at Available method section. Use below command for generating repositories :, (*5)

php artisan shareable:repositories <RepositoriesName> --model=<modelName>

eg : 

php artisan shareable:repositories UserRepositories --model=User

RepositoriesName - Repositories name, (*6)

modelName - Model name. This model were tied with repositories., (*7)

This command will create Repositories file inside App\Repositories directory on the fly. One repositories should have one model that tied with it. For ease of use, create repositories with descriptive name. Eg : UserRepositories, then we know that this repositories belongs to User model., (*8)

2) Generate Canal

Canal is a normal php class having all the reusable method that were created to call inside the controller. We didn't call the model directly inside controller instead we use this Canal class for that purpose to manage data passing/retreiving. This class should have methods that call Repositories's method., (*9)

php artisan shareable:canal <CanalName>

eg : 

php artisan shareable:canal UserModuleCanal

CanalName - Canal name., (*10)

This command will create Canal file inside App\Canal directory on the fly. This class should contains all the reusable code. As example we have UserModule for simple/complex crud operation, user permission and anything else. Then, all the methods can be placed inside UserModuleCanal, and UserModuleCanal can call any of Repositories's method inside of it. Later on, if we have two controller for the Web application and Api stuff with the same behaviour, we can just call the UserModuleCanal for both controller as they all share the same functionality., (*11)

3) Make use of traits inside Model

Last, include trait Arch\Util\Instantiate inside our Model that tied with the Repositories as follow :, (*12)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Arch\Util\Instantiate;

class User extends Model {
   use Instantiate;    
}

Follow these 3 steps for another module. Done!, (*13)

Usage Example

1) First, include Canal class inside Controller, eg : UserController :, (*14)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Canal\UserModuleCanal as UserCanal; // Canal class

class UserController extends Controller
{
    private $userModule = null;

    public function __construct () {
        // instantiate and assign to class member
        $this->userModule = new UserCanal;
    }
    // retrieving all users
    public function index () {

        dd( $this->userModule->getAllUser() );

    }
}

2) Create reusable code inside Canal class, eg : UserModuleCanal :, (*15)

<?php

namespace App\Canal;

use App\Repositories\UserRepositories as UserRepo; // include Repositories
use DB; // for creating custom query


class UserCanal {   

    // write method to call Repositories class
    public function getAllUser () {

        $user = new UserRepo; // Instantiate UserRepo object
        return $user->all();  // call UserRepo method and pass data back to Controller

    }

}

3) Simple example of Repositories, eg : UserRepositories :, (*16)

<?php

namespace App\Repositories;

use Arch\Repositories\Shareables\BaseShareables as BaseAbstract;
use App\User; // Model tied with this repositories
use DB;       // custom database querying

class UserRepositories extends BaseAbstract {   

    public function __construct() {
        // Assign to parent Model for data fetching
        $this->model = User::getInstance();
    }

    // At here we didn't create all() method, basically we didnt
    // create any method inside this repositories 
    // as all the laravel Query method already
    // available inside this repositories
    // by include this BaseShareables class
    // unless you need complex query that BaseShareables didnt provided for you

    // You can do any custom query from database in here
    // by creating custom method, later on, we can call
    // this method inside `Canal` class
}

4) Map the route to that controller and you're good to go., (*17)

Built-in Utilities/Libraries

  • Encryption (Using laravel encryption and Hashids by @ivanakimov)
  • MultiCurl
  • More utilities and libraries will added in future........

1) Encryption

Require the util inside controller directly after namespace keyword :, (*18)

use Arch\Libs\Fence, (*19)

Usage example :, (*20)

a) Encrypt data, (*21)

Return encrypted data, (*22)

$encrypted = Fence::encrypt( $var );

b) Decrypt data, (*23)

Return decrypted data, (*24)

$encrypted = Fence::decrypt( $var );

c) Hash, (*25)

Return hashed data, (*26)

$hashed = Fence::hash( $var );

d) Compare, (*27)

Return decrypted Data if success, otherwise false, (*28)

$hashed = 'KJNksdsjdhjunKLJ....';
$encrypted = 'SDxkzjxncjn...';
$encrypted = Fence::compare( $encrypted,$hashed );

e) Encode(Hashids), (*29)

Return encrypted data, (*30)

$encode = Fence::encode( $var ); // produce 15 length alpha numeric characters

f) Decode(Hashids), (*31)

Return decrypted data, (*32)

$decode = Fence::decode( $var );

g) Match(Hashids), (*33)

Return decrypted Data if success, otherwise false, (*34)

$encode = 'KJNksdsjdhjunKLJ....';
$hashed = 'SDxkzjxncjn...';
$hashed = Fence::match( $encode, $hashed );

2) MultiCurl

This util is same with MultiCurl as this package were required its automatically at package installation using composer. Require the util inside controller directly after namespace keyword :, (*35)

use Arch\Libs\MultiCurl, (*36)

Usage example : Example can be found directly at MultiCurl. The only different is you need to require the util using use Arch\Libs\MultiCurl instead of individual use Arch\MultiCurl\MultiCurl;. But both are the same. The choices is yours., (*37)

Supports

  • Well, just open an issues

Contribute

Feel free to fork and create PR, (*38)

Authors

The Versions

15/03 2017

dev-master

9999999-dev

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

13/03 2017

1.5.1

1.5.1.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

13/03 2017

1.5.0

1.5.0.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

03/02 2017

1.4.2

1.4.2.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

03/02 2017

1.4.1

1.4.1.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

03/02 2017

1.4.0

1.4.0.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

24/01 2017

1.3.1

1.3.1.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

24/01 2017

1.3.0

1.3.0.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

The Requires

 

by Avatar metallurgical
by AfiqAbdullah

03/01 2017

1.2.9

1.2.9.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

by Avatar metallurgical
by AfiqAbdullah

23/12 2016

1.2.8

1.2.8.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

by Avatar metallurgical
by AfiqAbdullah

23/12 2016

1.2.7

1.2.7.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

by Avatar metallurgical
by AfiqAbdullah

23/12 2016

1.2.6

1.2.6.0

Central repositories passing and receiving data between controller and model

  Sources   Download

MIT License

by Avatar metallurgical