API RESTFul desenvolvida com PHP, Mysql, autenticaĆ§Ć£o por JWT, CRUD de dados, rotas e entre outros.
Data de criaĆ§Ć£o: Jul 24, 2023
Features ā¢ How to User ? How to Consume API ?
API desenvolvida com PHP, Mysql, Rotas, URL amigĆ”vel, autenticaĆ§Ć£o por JWT, OOP e muito mais. O projeto consiste da criaĆ§Ć£o de usuĆ”rios, podendo cada usuĆ”rio, cadastrar, editar ou deletar seus livros.
- PHP
- JWT
- PDO (Mysql)
- OOP
- Routes
- SPL - Autoload
- MYSQL
- DDL
- DML
Segue-se alguns passos para a execuĆ§Ć£o da aplicaĆ§Ć£o:
-
Iniciar o servidor Apache e o Mysql.
-
Configure o arquivo config.php com suas credenciais de banco de dados e edite o BASE_URL conforme a localizaĆ§Ć£o do projeto.
-
Copie a pasta do projeto para dentro do servidor Apache.
-
Ativar o ModRewrite: comando via terminal:
a2enmod rewrite
ou habilitar nas configuraƧƵes do Apache. -
Executar os comandos DDL e DML do arquivo database.sql, o arquivo se encontra na raiz do projeto.
Nos exemplos de consumo da API, serĆ” utilizado a funcionalidade Fetch API do Javascript.
/**
* @Route("/", methods: {"GET"})
*/
fetch('http://127.0.0.1/myBooksAPI/')
.then(res => res.json())
.then(console.log)
/* {
"message": "Hey There! š¦",
"guide": "https://github.com/EricNeves/myBooksAPI"
} */
/**
* @Route("/users/create", methods: {"POST"})
*/
const config = {
method: 'POST',
body: JSON.stringify({ name, email, password })
}
fetch('http://127.0.0.1/myBooksAPI/users/create', config)
.then(res => res.json())
.then(console.log)
/* {
login: "http://127.0.0.1/github/myBooksAPI/users/login",
message: "Created"
} */
/**
* @Route("/users/login", methods: {"POST"})
*/
const config = {
method: 'POST',
body: JSON.stringify({ email, password })
}
fetch('http://127.0.0.1/myBooksAPI/users/login')
.then(res => res.json())
.then(console.log)
/* {
message: "successfully",
token: your-jwt-token
} */
/**
* @Route("/users", methods: {"GET"})
*/
const config = {
method: 'GET',
headers: {
Authorization: 'Bearer ${your-jwt-token}'
}
}
fetch('http://127.0.0.1/myBooksAPI/users', config)
.then(res => res.json())
.then(console.log)
/* {
data: {
"id": your-id,
"name": your-name,
"email: your-email,
}
} */
/**
* @Route("/users/update", methods: {"PUT"})
*/
const config = {
method: 'PUT',
body: JSON.stringify({ name, password })
headers: {
Authorization: 'Bearer ${your-jwt-token}'
}
}
fetch('http://127.0.0.1/myBooksAPI/users/update', config)
.then(res => res.json())
.then(console.log)
/* {message: 'User updated'} */
/**
* @Route("/books", methods: {"GET"})
*/
const config = {
method: 'GET',
headers: {
Authorization: 'Bearer ${your-jwt-token}'
}
}
fetch('http://127.0.0.1/myBooksAPI/books')
.then(res => res.json())
.then(console.log)
/* {
"quantity": 0,
"books": []
} */
/**
* @Route("/books/create", methods: {"POST"})
*/
const config = {
method: 'POST',
body: JSON.stringofy({ title, year }),
headers: {
Authorization: 'Bearer ${your-jwt-token}'
}
}
fetch('http://127.0.0.1/myBooksAPI/books/create')
.then(res => res.json())
.then(console.log)
/* { "message": "Book created" } */
/**
* @Route("/books/list/{book_id}", methods: {"GET"})
*/
const config = {
method: 'GET',
headers: {
Authorization: 'Bearer ${your-jwt-token}'
}
}
fetch('http://127.0.0.1/myBooksAPI/books/list/{book_id}')
.then(res => res.json())
.then(console.log)
/* {
book: {
"id": book_id,
"title": book_title,
"year_created": book_year,
"user_id": user_id,
}
} */
/**
* @Route("/books/update/{book_id}", methods: {"PUT"})
*/
const config = {
method: 'PUT',
body: JSON.stringify({ title, year }),
headers: {
Authorization: 'Bearer ${your-jwt-token}'
}
}
fetch('http://127.0.0.1/myBooksAPI/update/{book_id}')
.then(res => res.json())
.then(console.log)
/* {message: 'Book updated'} */
/**
* @Route("/books/remove/{book_id}", methods: {"DELETE"})
*/
const config = {
method: 'DELETE',
headers: {
Authorization: 'Bearer ${your-jwt-token}'
}
}
fetch('http://127.0.0.1/myBooksAPI/books/remove/{book_id}')
.then(res => res.json())
.then(console.log)
/* {message: 'Book deleted'} */