Download the PHP package zozlak/rest without Composer

On this page you can find all versions of the php package zozlak/rest. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package rest

REST

Build Status Coverage Status

Set of PHP classes for REST APIs creation:

Features

Installation

The easiest way to install is by using composer:

Sample usage

Lets assume you want to create a RESTfull API providing following endpoints:

HTTP server configuration

At first you will probably want to configure your HTTP server to redirect requests coming to your API to one PHP file. Sample set of rules for Apache (to put into the .htaccess file or into a <VirtualHost> configuration directive) would be:

Endpoint classes

zozlak\rest\HttpEndpoint is a base class for implementing REST API endpoints. It provides a default implementation (emit HTTP 501 method not implemented error code) for all HTTP methods for both resource and collection endpoints (e.g. both http://yourDomain/person and http://yourDomain/person/{id}). For resource endpoint method names simply follow HTTP method names (get(), put(), etc.) and for the collection endpoint they are suffixed with a collection (getCollection(), postCollection(), etc.). The dfault implementation of the OPTION method checks which methods are implemented in your class and emits the Allow header value accordingly (and the 404 Not Found if none implemented).

You should derive your class from the zozlak\rest\HttpEndpoint one and override methods with useful implementations.

A class name should follow the name of the last API endpoint segment (skipping {id} segments) converted to CameCase, e.g.:

This means we must create two classes: Person and Project. Lets assume you will put their code into the src directory and follow the PSR-4 naming rules (meaning file name follows class name).

src\Person.php

src\Project.php

Just copy-paste-adapt the src\Person.php.

index.php

Test your API

Try to access your API endpoints, e.g. with curl:

Advanced topics

Authorization

To check credentials provided by the user use getAuthUser() and getAuthPswd() in your endpoint's class code. If the credentials are wrong simply throw the zozlak\rest\UnauthorizedException exception.

Example:

Changing HTTP status code and headers

Use the HeadersFormatter object passed to all methods to alter response HTTP status and headers.

Lets assume you want to return for the POST method run on a collection with a 201 Created response code and a Location header pointing to the newly created item:

There is also a shorthand for redirects:

Returning non-JSON data

You shouldn't directly return data with echo, etc. because it can break some library features (precisely after printing more then PHP output buffer bytes the PHP will automatically issue HTTP 200 status code with default set of HTTP headers and all the settings made to the HeadersFormatter will be discarded).

There are two helper methods of the DataFormatter object passed all methods helping you to deal with non-JSON data:

To alter HTTP status code and/or add other headers, use the setStatus() and setHeader() methods on the HeadersFormatter object.

Example:


All versions of rest with dependencies

PHP Build Version
Package Version
Requires php Version >=8
zozlak/util Version ^1.5.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zozlak/rest contains the following files

Loading the files please wait ....