Share
Embed
Desenvolvendo APIs com PHP e Mezzio
Teoria e uma demo prática de como desenvolver APIs web usando PHP e Mezzio.
Er Galvão Abbott
June 17, 2020
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
57 slides
-
www.galvao.eti.br CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 1 / 57 Desenvolvendo APIs com PHP & Mezzio ElePHPant by Vincent Pontier. Laminas Mezzio logo by Laminas Project & APIs com HTTP/2 200 OK php -
Presidente da ABRAPHP – Associação Brasileira de Profissionais PHP Diretor
da ConFLOSS Diretor da PHP Conference Brasil Evangelista e Contribuidor para a Linguagem Evangelista Laminas Project (auto-intitulado) Embaixador Fedora LATAM Professor Convidado (Especialista) de Pós-Graduação (PR e SC) 25+ anos desenvolvendo sistemas com interface web 20+ destes com PHP 12+ com Zend Framework Palestrante em eventos nacionais e internacionais Professor de cursos presenciais e a distância Quem?! CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 6/17/20 - 2 / 57 www.galvao.eti.br www.galvao.eti.br Desenvolvendo APIs com PHP & Mezzio -
Objetivo CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 3 / 57 www.galvao.eti.br Expor detalhes envolvidos na criação de uma API Web e demonstrar a criação de um exemplo utilizando PHP7 e Mezzio1. [1] https://www.galvao.eti.br/do-zend-framework-ao-laminas/ Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 4 / 57 www.galvao.eti.br Application Programming Interface Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 5 / 57 www.galvao.eti.br Application Programming Interface para a web, ou seja: Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 6 / 57 www.galvao.eti.br Application Programming Interface Recurso http[s]:// Endereço Dados Método [ ] para a web, ou seja: Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 7 / 57 www.galvao.eti.br “Mas isso é simplesmente a Web!” Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 8 / 57 www.galvao.eti.br “Mas isso é simplesmente a Web!” Sim ... e não. Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 9 / 57 www.galvao.eti.br “Mas isso é simplesmente a Web!” Sim ... e não. APIs Web: Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 10 / 57 www.galvao.eti.br “Mas isso é simplesmente a Web!” Sim ... e não. APIs Web: São não-interativas* Desenvolvendo APIs com PHP & Mezzio -
API Web? CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 11 / 57 www.galvao.eti.br “Mas isso é simplesmente a Web!” Sim ... e não. APIs Web: São não-interativas* Retornam dados, sem apresentação Desenvolvendo APIs com PHP & Mezzio -
Anatomia CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 12 / 57 www.galvao.eti.br Desenvolvendo APIs com PHP & Mezzio -
Duas Validações?! CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 13 / 57 www.galvao.eti.br 1ª Validação: Vale a pena processar esta Requisição? 2ª Validação: Resultado do processamento Desenvolvendo APIs com PHP & Mezzio -
Anatomia – PSR CC Attribution-ShareAlike 3.0 Unported License by Er
Galvão Abbott - 6/17/20 - 14 / 57 www.galvao.eti.br 1ª Validação Processamento / 2ª Validação Desenvolvendo APIs com PHP & Mezzio -
FFUC – Frequently F ed Up Concepts CC Attribution-ShareAlike 3.0
Unported License by Er Galvão Abbott - 6/17/20 - 15 / 57 www.galvao.eti.br *** NÃO existe requisição sem identificação Desenvolvendo APIs com PHP & Mezzio -
FFUC – Frequently F ed Up Concepts CC Attribution-ShareAlike 3.0
Unported License by Er Galvão Abbott - 6/17/20 - 16 / 57 www.galvao.eti.br *** NÃO existe requisição sem identificação Evite usar Status Codes 5xx! Desenvolvendo APIs com PHP & Mezzio -
FFUC – Frequently F ed Up Concepts CC Attribution-ShareAlike 3.0
Unported License by Er Galvão Abbott - 6/17/20 - 17 / 57 www.galvao.eti.br *** NÃO existe requisição sem identificação Sucessos NEM SEMPRE geram Status 200 Evite usar Status Codes 5xx! Desenvolvendo APIs com PHP & Mezzio -
FFUC – Frequently F ed Up Concepts CC Attribution-ShareAlike 3.0
Unported License by Er Galvão Abbott - 6/17/20 - 18 / 57 www.galvao.eti.br *** NÃO existe requisição sem identificação Sucessos NEM SEMPRE geram Status 200 Erros NUNCA geram Status 200 Evite usar Status Codes 5xx! Desenvolvendo APIs com PHP & Mezzio -
FFUC – Frequently F ed Up Concepts CC Attribution-ShareAlike 3.0
Unported License by Er Galvão Abbott - 6/17/20 - 19 / 57 www.galvao.eti.br *** NÃO existe requisição sem identificação Sucessos NEM SEMPRE geram Status 200 Erros NUNCA geram Status 200 Erros NUNCA geram Status 200 Evite usar Status Codes 5xx! Desenvolvendo APIs com PHP & Mezzio -
FFUC – Frequently F ed Up Concepts CC Attribution-ShareAlike 3.0
Unported License by Er Galvão Abbott - 6/17/20 - 20 / 57 www.galvao.eti.br *** NÃO existe requisição sem identificação Sucessos NEM SEMPRE geram Status 200 Erros NUNCA geram Status 200 Erros NUNCA geram Status 200 Erros NUNCA geram Status 200 Evite usar Status Codes 5xx! Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 21 / 57 www.galvao.eti.br Recurso criado? Processamento longo ou particionado? Sem contéudo na resposta? Outros casos 201 202* 204 200** Sucessos Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 22 / 57 www.galvao.eti.br Recurso existe? Método Válido? Número de Requisições válido? Headers OK? Autenticação necessária, provida e válida? Usuário autorizado? Tamanho de Payload >= mínimo? Tamanho de Payload <= máximo? Payload válido? Conteúdo de resposta válido? 404 405 429 400* 401 403 400* 413 400* 406 1ª Validação 2ª Validação Erros Desenvolvendo APIs com PHP & Mezzio -
Recurso existe? Método Válido? Número de Requisições válido? Headers OK?
Autenticação necessária, provida e válida? Usuário autorizado? Tamanho de Payload >= mínimo? Tamanho de Payload <= máximo? Payload válido? Conteúdo de resposta válido? 404 405 429 400* 401 403 400* 413 400* 406 1ª Validação 2ª Validação CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 6/17/20 - 23 / 57 www.galvao.eti.br Erros Fácil... Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 24 / 57 www.galvao.eti.br … ou melhor ... Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 25 / 57 www.galvao.eti.br … ou melhor ... Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 26 / 57 www.galvao.eti.br … ou melhor ... https://github.com/galvao/Flow (WiP) Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 27 / 57 www.galvao.eti.br Exemplo Skeleton Roteamento Handler Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 28 / 57 www.galvao.eti.br Use código-fonte que você não entende completamente e prepare-se para ser julgado DISCLAIMER Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 29 / 57 www.galvao.eti.br Use código-fonte que você não entende completamente e prepare-se para ser julgado pelo Rei Koala do Inferno. DISCLAIMER Desenvolvendo APIs com PHP & Mezzio -
Skeleton CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 30 / 57 www.galvao.eti.br Inicialização da Skeleton e opções de instalação podem ser vistas em: https://asciinema.org/a/340031 composer create-project \ mezzio/mezzio-skeleton \ /var/www/html/sample_api Desenvolvendo APIs com PHP & Mezzio -
Roteamento CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 31 / 57 www.galvao.eti.br /var/www/html/sample_api/config/routes.php Desenvolvendo APIs com PHP & Mezzio -
Roteamento CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 32 / 57 www.galvao.eti.br Múltiplos Métodos Único Método Desenvolvendo APIs com PHP & Mezzio -
Roteamento CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 33 / 57 www.galvao.eti.br $app delete connect get head options patch post put trace route (‘/segmento-de-url’, middleware(s), ‘identificador’ ) (‘/segmento-de-url’, middleware(s), [método(s)], ‘identificador’ ) Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 34 / 57 www.galvao.eti.br Handler /var/www/html/sample_api/src/App/src/Handler/HomePageHandler.php Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 35 / 57 www.galvao.eti.br Handler /var/www/html/sample_api/src/App/src/Handler/HomePageHandler.php Construtor Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 36 / 57 www.galvao.eti.br Handler Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 37 / 57 www.galvao.eti.br Handler /var/www/html/sample_api/src/App/src/Handler/HomePageHandler.php Handle Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 38 / 57 www.galvao.eti.br Handler Desenvolvendo APIs com PHP & Mezzio -
Respostas CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 39 / 57 www.galvao.eti.br return new Html Empty Json Redirect Text Response( ); ... * https://docs.laminas.dev/laminas-diactoros/v2/custom-responses/ … e mais* Desenvolvendo APIs com PHP & Mezzio -
Testando CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 40 / 57 www.galvao.eti.br ? ? ? Desenvolvendo APIs com PHP & Mezzio -
Testando CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 41 / 57 www.galvao.eti.br ? ? ? Desenvolvendo APIs com PHP & Mezzio -
Testando CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 42 / 57 www.galvao.eti.br $ http GET http://sample_api/ Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 43 / 57 www.galvao.eti.br Adicionando Dados Instalação / Configuração Injeção de Dependência Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 44 / 57 www.galvao.eti.br Instalação / Configuração config/autoload/database.local.php $ composer require laminas/laminas-db Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 45 / 57 www.galvao.eti.br Injeção config/autoload/dependencies.global.php Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 46 / 57 www.galvao.eti.br Execução via Factory src/App/src/Handler/HomePageHandlerFactory.php Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 47 / 57 www.galvao.eti.br Execução via Factory src/App/src/Handler/HomePageHandlerFactory.php Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 48 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 49 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 50 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 51 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 52 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 53 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 54 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
6/17/20 - 55 / 57 www.galvao.eti.br Execução via Factory Desenvolvendo APIs com PHP & Mezzio -
Links CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 6/17/20 - 56 / 57 www.galvao.eti.br Documentação Mezzio https://docs.mezzio.dev/ Laminas Diactoros https://docs.laminas.dev/laminas-diactoros/ RFC 7231 #Status Codes https://tools.ietf.org/html/rfc7231#section-6 RFC 7540 https://tools.ietf.org/html/rfc7540 PSRs #7, 15, 17 e 18 https://www.php-fig.org/psr/ RTFM Ferramentas HTTPie Postman jq Recursos APIs you won’t hate https://apisyouwonthate.com/ API Evangelist http://apievangelist.com/ Desenvolvendo APIs com PHP & Mezzio -
Muito obrigado! CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 6/17/20 - 57 / 57 www.galvao.eti.br Dúvidas? Críticas? Elogios?! ? Desenvolvendo APIs com PHP & Mezzio