wafto/laravel-sepomex

Servicio Postal Mexicano (Sepomex) laravel library

v2.0.0 2021-03-24 20:14 UTC

This package is auto-updated.

Last update: 2024-04-04 20:07:45 UTC


README

Tests Total Downloads Latest Stable Version License

Introduction

This package provides postal code SEPOMEX information (unofficial) for Laravel.

Installation

You can install this package by running:

composer require wafto/laravel-sepomex

For Laravel versions below 6.0 please use the version tagged with 1.6.0.

Setup

In order to setup this package, the next steps are needed.

1) Service Provider

If you don't use auto-discovery (Laravel 5.5+), add the SepomexServiceProvider to the providers array in config/app.php.

Aftab\Sepomex\SepomexServiceProvider::class

2) Configuration file

Publish the sepomex.php configuration file under app/config using the following command:

php artisan vendor:publish --provider="Aftab\Sepomex\SepomexServiceProvider"

Here the configuration like table_name and source_file can be changed.

3) Source file

Download and copy file datos.gob.mx in the storage directory as cpdescarga.txt, note that this path should match with the config file.

4) Migrate and Import

After configuring the file path and table name, run migrations and run the importer command.

php artisan migrate
php artisan sepomex:import --chunk=50

This step might took some time to finish.

Usage

Only inject the contract Aftab\Sepomex\Contracts\SepomexContract or use app(Aftab\Sepomex\Contracts\SepomexContract::class) to get the singleton instance.

...
use Aftab\Sepomex\Contracts\SepomexContract;

class SepomexController extends Controller
{
    public function postal(SepomexContract $sepomex, $postal)
    {
        return $sepomex->getByPostal($postal);
    }

    public function states(SepomexContract $sepomex)
    {
        return $sepomex->getStates();
    }
}

The second option is to use the Sepomex Facade by editing the config/app.php by adding the alias.

'aliases' => [
    ...
    'Sepomex' => Aftab\Sepomex\Facades\Sepomex::class,
]

And using anywhere in your application.

...

class SepomexController extends Controller
{
    public function postal($postal)
    {
        return Sepomex::getByPostal($postal);
    }

    public function states()
    {
        return Sepomex::getStates();
    }
}

Important Notes

The database is distributed with a restrictive clause on the first line of the file.

El Catálogo Nacional de Códigos Postales, es elaborado por Correos de México y se proporciona en forma gratuita para uso particular, no estando permitida su comercialización, total o parcial, ni su distribución a terceros bajo ningún concepto.

But the database has been released under the license LIBRE USO MX so hopefully in the future the clause in the file change.

Source (Fuente)

"Catálogo Nacional de Códigos Postales" publicado por Correos de México. Consultado en https://datos.gob.mx/busca/dataset/catalogo-nacional-de-codigos-postales el 2017-11-06.

License

Laravel Sepomex is open-sourced software licensed under the MIT license.