Giter Site home page Giter Site logo

cakephp3restapi's Introduction

CakePHP 3 REST API

A skeleton for creating a REST API using CakePHP 3 with the awesome plugins JWT Authentication and CRUD. This API was built using the amazing Bravo's Kernel tutorial.

Installation

Firstly, you must create a database and set it on config/app.php file. After that, must run the migrations, that will create the example tables:

$ bin/cake migrations migrate

After that, if all goes well, the API can now be tested through a REST Client as the Postman, for Chrome.

You can access or retrieve data in two ways: by API or browser request. By the way, in this project, only the API requires some authorization (by JWT, but I'll explain later), if you try to access the data by browser request, you will get it. For example:

http://localhost/restful/cocktails

It will retrieve the list of cocktails existing in the database and display it in the browser. To access the API you must use the "API" prefix in the URL, as in the example below:

http://localhost/restful/api/cocktails

To get permission, you need to have a valid JWT. To get one, you must follow the following steps. With the chosen REST Client, use these settings:

Url: http://localhost/restful/api/users/register
Http method: POST
Accept Header application/json
Content-Type Header application/json
Body: data with username and password in JSON format. For example:

{
	"username": "patrick",
	"password": "test123",
	"active": true
}

If all goes well, a success message 201 will be returned via JSON:

{
    "success": true,
    "data": {
        "id": 2,
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MiwiZXhwIjoxNDQ1NjA4MjM1fQ.7YJifOL0nQf3XwR1XWIZ969IMzLbFuYLniEEojVjoqA"
    }
}

Accessing the API

After obtaining your token, you will get permission to access all the API, using the Authorization Header with the generated JWT, with the following format:

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MiwiZXhwIjoxNDQ1NjA4MjM1fQ.7YJifOL0nQf3XwR1XWIZ969IMzLbFuYLniEEojVjoqA

Index (retrieve all cocktails):

URL http://cake3api.app/api/cocktails
HTTP Method GET
Accept Header application/json
Authorization Header containing Bearer {YOUR-JWT-TOKEN}

View (retrieve a specific cocktail):

URL http://cake3api.app/api/cocktails/5
HTTP Method GET
Accept Header application/json
Authorization Header containing Bearer {YOUR-JWT-TOKEN}

Add (create a new cocktail):

URL http://cake3api.app/api/cocktails
HTTP Method POST
Accept Header application/json
Content-type Header aplication/json
Authorization Header containing Bearer {YOUR-JWT-TOKEN}
Body in JSON format:
{ 
    "name": "new cocktail",
	"description": "description"
}

Edit (update a cocktail info):

URL http://cake3api.app/api/cocktails/5
HTTP Method PUT
Accept Header application/json
Content-type Header aplication/json
Authorization Header containing Bearer {YOUR-JWT-TOKEN}
Body in JSON format (partial or all content):
{ 
	"description": "new description"
}

Delete:

URL http://cake3api.app/api/cocktails/5
HTTP Method DELETE
Accept Header application/json
Authorization Header containing Bearer {YOUR-JWT-TOKEN}

This project is completely extensible and was built with the premise to be a case study, so you can create your own controllers using this skeleton as a base.

cakephp3restapi's People

Contributors

patarkf avatar

Watchers

James Cloos avatar  avatar

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.