oanhnn/laravel-fakeid

Using fake id on URL in Laravel application

v2.0.0 2020-07-14 05:01 UTC

This package is auto-updated.

Last update: 2024-12-14 14:14:50 UTC


README

Build Status Coverage Status Latest Version Total Downloads Requires PHP Software License

Easy fake model ID on URL Laravel Application.

Requirements

  • php >=7.2
  • Laravel 7.0+

Laravel 5.5+ using version 1.x (require php 7.1.3+)

Installation

Begin by pulling in the package through Composer.

$ composer require oanhnn/laravel-fakeid

Publish config file with

$ php artisan vendor:publish --provider="Laravel\\FakeId\\ServiceProvider"

or

php artisan vendor:publish --tag=laravel-fakeid-config

Edit config/fakeid.php for config specific drivers.

Usage

Getting start

In your model class, add implement interface ShouldFakeId and a trait RoutesWithFakeId:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\FakeId\Contracts\ShouldFakeId;
use Laravel\FakeId\RoutesWithFakeId;

class MyModel extends Model implements ShouldFakeId
{
    use RoutesWithFakeId;
 
    // other logic
}

Using specific driver

By default, RoutesWithFakeId use default driver, it is set in config file. You can override getFakeIdDriver() method to use specific driver:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\FakeId\Contracts\ShouldFakeId;
use Laravel\FakeId\Contracts\Driver;
use Laravel\FakeId\Facades\FakeId;
use Laravel\FakeId\RoutesWithFakeId;

class MyModel extends Model implements ShouldFakeId
{
    use RoutesWithFakeId;

    /**
     * @return \Laravel\FakeId\Contracts\Driver
     */
    public function getFakeIdDriver() : Driver
    {
        return FakeId::driver('hex');
        // or create driver instance
        // return new HexDriver();
    }
}

Note With each drivers, the input data format may be different, but the output data after decode is same with input

Custom driver

You can also create custom driver by implements Laravel\FakeId\Contracts\Driver interface

namespace App;

use Laravel\FakeId\Contracts\Driver;

class CustomDriver implements Driver
{
    // your driver logic
}    

And register with FakeId Manager by add below code to AppServiceProvider::boot() method

<?php

namespace App\Providers;

use App\CustomDriver;
use Illuminate\Support\ServiceProvider;
use Laravel\FakeId\Facades\FakeId;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        FakeId::extend('custom', function($app) {
            return new CustomDriver();
        });

        // other logic
    }
}

Now, you can use it

<?php

use Laravel\FakeId\Facades\FakeId;

FakeId::driver('custom')->encode('123');

Changelog

See all change logs in CHANGELOG

Testing

$ git clone git@github.com/oanhnn/laravel-fakeid.git /path
$ cd /path
$ composer install
$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email to Oanh Nguyen instead of using the issue tracker.

Credits

License

This project is released under the MIT License.
Copyright © Oanh Nguyen.