Laravel pacote do Web Service VIACEP web service - http://viacep.com.br
Utilizando composer: Composer
composer require canducci/zipcode
Adicione as classes no final do array de providers
no arquivo config/app.php
:
'providers' => [
// ...,
Canducci\ZipCode\Providers\ZipCodeServiceProvider::class,
Canducci\ZipCode\Providers\ZipCodeAddressServiceProvider::class,
],
e adicione os seus apelidos no array aliases
:
'aliases' => [
// ...,
'ZipCode' => Canducci\ZipCode\Facades\ZipCode::class,
'Address' => Canducci\ZipCode\Facades\ZipCodeAddress::class,
],
para finalizar precisa dar um:
php artisan vendor:publish
após digitar esse comando vai aparecer um menu de opções então escolha Canducci\ZipCode\Providers\ZipCodeServiceProvider
para publicar o arquivo de configuração (simplecache.php
) na pasta config/
Temos 4 caminhos para usufruir desse pacote:
use Canducci\ZipCode\Facades\ZipCode;
$zipCodeInfo = ZipCode::find('01414-001');
$zipCodeInfo = zipcode('01414000');
use Canducci\ZipCode\Contracts\ZipCodeContract;
public function index(ZipCodeContract $zipcode)
{
$zipCodeInfo = $zipcode->find('01414-000');
}
use Canducci\ZipCode\ZipCodeTrait;
class WelcomeController extends Controller
{
use ZipCodeTrait;
public function index()
{
$zipCodeInfo = $this->zipcode('01414000');
}
}
Há diversas formas de chegar no mesmo resultado:
$zipCodeInfo = ZipCode::find('01414000'); // Facade
$zipCodeInfo = $zipcode->find('01414-000'); // Contract
$zipCodeInfo = zipcode('01414000'); // Helper
$zipCodeInfo = $this->zipcode('01414-000'); // Trait
Pode forçar um item a renovar seu cache com o segundo parâmetro:
$zipCodeInfo = ZipCode::find('01414000', true); // Facade
$zipCodeInfo = $zipcode->find('01414-000', true); // Contract
$zipCodeInfo = zipcode('01414000', true); // Helper
$zipCodeInfo = $this->zipcode('01414-000', true); // Trait
Por padrão o retorno é nulo ou a instância da classe Canducci\ZipCode\ZipCodeInfo
, e com esse retorno de classe existe os tipos array
, object
e json
texto:
if ($zipCodeInfo) // null or ZipCodeInfo
{
$arr = $zipCodeInfo->getArray(); // Array
$json = $zipCodeInfo->getJson(); // Json
$obj = $zipCodeInfo->getObject(); // Object
}
if ($zipCodeInfo)
{
$arr = $zipCodeInfo->getArray();
/*
Array
(
[cep] => 01414-001
[logradouro] => Rua Haddock Lobo
[bairro] => Cerqueira César
[localidade] => São Paulo
[uf] => SP
[ibge] => 3550308,
[complemento] =>
[gia] => 1004,
[siafi] => 0
[ddd] = 11,
[unidade] = '',
[estado] = '',
[regiao] = ''
)
*/
}
if ($zipCodeInfo)
{
$json = $zipCodeInfo->getJson();
/*
{
"cep": "01414-001",
"logradouro": "Rua Haddock Lobo",
"bairro": "Cerqueira César",
"localidade": "São Paulo",
"uf": "SP",
"ibge": "3550308",
"complemento": ""
"gia": 1004,
"ddd": "11",
"siafi": 0,
"unidade": '',
"estado": '',
"regiao": ''
}
*/
}
if ($zipCodeInfo)
{
$obj = $zipCodeInfo->getObject();
/*
stdClass Object
(
[cep] => 01414-001
[logradouro] => Rua Haddock Lobo
[bairro] => Cerqueira César
[localidade] => São Paulo
[uf] => SP
[ibge] => 3550308
[complemento] =>
[gia] => 1004
[siafi] => 0
[ddd] = 011,
[unidade] = '',
[estado] = '',
[regiao] = ''
)
*/
}
public function get(Request $request)
{
$uf = $request->get('uf');
$city = $request->get('cidade');
$address = $request->get('endereco')
$zipcodeaddressinfo = zipcodeaddress($uf, $city, $address);
if ($zipcodeaddressinfo)
return $zipcodeaddressinfo->getJson();
return Response::json(['error' => 1]);
}
use Canducci\ZipCode\ZipCodeUf;
$lists = ZipCodeUf::lists();