Skip to content

ddimaria/Laravel-REST-CMS

Repository files navigation

Larevel REST CMS

A Laravel 5 based REST Server for Content Management

Software License Build Status Scrutinizer Code Quality Coverage Status Latest Version

This package complies with PSR-1, PSR-2 and PSR-4.

Requirements

  • PHP >= 5.5.9
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension

Installation

After ensuring that you meet the above requirements, follow the below procedures for installing Laravel REST CMS

Clone this repo

$ git clone https://github.com/ddimaria/Laravel-REST-CMS.git laravel-rest-cms
$ cd laravel-rest-cms

Run Composer

This assumes you have composer installed and configured to run globally. For assistance, visit https://getcomposer.org/download/

$ composer install

This creates a /vendor directory and will pull in dependenies.

Folder Permissions

$ find storage/* -type d -exec chmod 775 {} \;

Environment Configuration

$ cp .env.example to .env

After this file copy, update the attributes in .env to match your environment, database, and mail configurations.

Create a Unique Encryption Key

$ php artisan key:generate

Migrate the Database

$ php artisan migrate

Seed the Database

$ php artisan db:seed

Testing

Basic

$ phpunit

With Coverage HTML

The testing goal is 100% covearge of non-vendor, non-laravel code.

$ phpunit --coverage-html --coverage-clover=coverage.clover

Packages and General Processes

API Design

This system uses the thephpleague/fractal component, which is "a presentation and transformation layer for complex data output." This provides a solid foundation for relating models, transforming data, pagination responses and standardizing input parameters.

Responses

Responses are sent using the ellipsesynergie/api-response package. This ties into Fractal's Manager object for simplifying and standardizing responses.

Authentication

The system implements token-based authetication with the chrisbjr/api-guard component. This nifty package plays well with Fractal and Api-Response, and fully abstracts authentication, token generation and maintenence, api rate limiting, access levels, method-level access and full api logging.

The token name is X-Authorization, and is used to authenticate requests. To authenticate, add it to the header of your request with the api_key value you receive after logging in. There are some routes that do not require api authentication.

curl --header "X-Authorization: 2ed9d72e5596800bf805ca1c735e446df72019ef" http://localhost:8000/api/v1/page/1

Pagination

When returning data for collection-based endpoints, results are paginated, 15 per page.

"meta": {
    "pagination": {
        "total": 150,
        "count": 15,
        "per_page": 15,
        "current_page": 3,
        "total_pages": 10,
        "links": {
            "previous": "https://localhost/api/v1/pages/?page=2",
            "next": "https://localhost/api/v1/pages/?page=4"
        }
    }
}

Error Responses

404 responses are returned with a 404 status code and a "Not found!" JSON response:

{
    "error": {
        "message": "Not found!",
        "status_code": 404
    }
}

Validation errors throw a 422 response:

{
  "error": {
    "code": "GEN-UNPROCESSABLE-ENTITY",
    "http_code": 422,
    "message": [
      "The name field is required.",
      "The layout field is required."
    ]
  }
}

Caching

All models that extend \App\LaravelRestCms\BaseModel implement the \App\LaravelRestCms\CacheTrait in which are cached when saved.

Usage

Logging In

POST /app/v1/user/login

POST

{
	"username": "admin", 
	"password": "123"
}

Response:

{
  "data": {
    "id": 1,
    "first_name": "Admin",
    "last_name": "User",
    "api_key": "7fa1949b94f9000f4bb558709aee106f8c0d042c",
    "version": "version: 1.0.3"
  }
}

Logging Out

GET /app/v1/user/logout/{api_key}

Response

{
  "ok": {
    "code": "SUCCESSFUL",
    "http_code": 200,
    "message": "User was successfuly deauthenticated"
  }
}

Simple Page

GET /app/v1/page/{id}

Response

{
  "data": {
    "id": 1,
    "parent_id": 0,
    "template_id": 1,
    "nav_name": "Home",
    "url": "home",
    "title": "Home Page"
  }
}

Page data, including page_detail and template_detail joins

GET /app/v1/page/{id}/detail GET /app/v1/page/{slug}

The 2 routes above get the same information. The first one keys off of the primary key (id), while the second one looks up the page by the slug.

Response

{
  "data": {
    "id": 1,
    "parent_id": 0,
    "template_id": 1,
    "nav_name": "Home",
    "url": "home",
    "title": "Home Page",
    "detail": {
      "data": [
        {
          "id": 1,
          "page_id": 1,
          "template_detail_id": 1,
          "data": "First page content",
          "group": 0,
          "version": 0,
          "template_detail": {
            "data": [
              {
                "id": 1,
                "parent_id": 0,
                "name": "Main Content",
                "description": null,
                "var": "main_content",
                "type": "wysiwyg",
                "data": null,
                "sort": 1
              }
            ]
          }
        },
        {
          "id": 2,
          "page_id": 1,
          "template_detail_id": 2,
          "data": "First page sub-content",
          "group": 0,
          "version": 0,
          "template_detail": {
            "data": [
              {
                "id": 2,
                "parent_id": 1,
                "name": "Sub Content",
                "description": null,
                "var": "main_content",
                "type": "wysiwyg",
                "data": null,
                "sort": 1
              }
            ]
          }
        }
      ]
    },
    "parent": {
      "data": []
    }
  }
}

Create a Page

POST /app/v1/page/{id}

POST

{
    "parent_id" : 1,
    "template_id" : 1,
    "nav_name": "New Page",
    "url": "new-page-here",
    "title" : "New Page Title"
}

Response

{
  "data": {
    "id": 21,
    "parent_id": 1,
    "template_id": 1,
    "nav_name": "New Page",
    "url": "new-page-here",
    "title": "New Page Title"
  }
}

License

The MIT License (MIT). Please see License File for more information.

About

A Laravel 5 based REST Server for Content Management

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages