Giter VIP home page Giter VIP logo

laravel-urn's Introduction

Laravel Urn

Simple way to create urn identifiers for your laravel application, allowing a simple way to identifies resources or models a more descriptive way.

alt text

Installation

Application can be installed by running:

composer require serendipias/laravel-urn

The application requires laravel/lumen >= 6.x

Usage

Register provider:

In Laravel config/app.php add the urn service provider to your providers array:

\Serendipias\Urn\UrnServiceProvider::class,

if you are using Lumen you will need to register the service provider in your bootstrap/app.php

$app->register(\Serendipias\Urn\UrnServiceProvider::class);
Publishing configuration:

Publish the configuration, run the following command:

php artisan vendor:publish --tag=urn

In lumen you will need to manually copy the urn file to your config directory.

The package has 3 basic configurations that are required to work with laravel/lumen models:

  • models_namespace: Models namespace, this is use to be able to get models instances from the urn
  • service: this is use to declare where the urn was created, which application is the source for the identifier
  • stage: determinate if the resource was generated in production, testing, stage or unit testing.
Enable models to generate urns

To allow a model to be urn identifiable the model must extend Serendipias\Urn\Contracts\UrnIdentifiable interface, the package provides a trait with the interface default implementations UrnIdentifiableTrait.

To generate urn with a model, inject or make Serendipias\Urn\Services\UrnService::class, from there you can encode a model, or decode an urn string, in both cases an Serendipias\Urn\Support\Urn is returned.

Urn instance example:

Serendipias\Urn\Support\Urn {
  -urn: "urn:laravel:local:user:1"
  -namespace: "App\Models"
  -parts: array:5 [
    0 => "urn"
    1 => "laravel"
    2 => "local"
    3 => "user"
    4 => "1"
  ]
}

To encode an model:

$service = app(\Serendipias\Urn\Services\UrnService::class);
$urn = $service->encode(\App\Models\User::find(1));
$urn->getUrnString(); // urn:laravel:local:user:1

To decode a urn string:

$service = app(\Serendipias\Urn\Services\UrnService::class);
$urn = $service->decode('urn:laravel:local:user:1');
$urn->find(); // will return User model or null if not found

laravel-urn's People

Contributors

coquer avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

tanviralam

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.