Getting Started

A simple tutorial to get started with BeRest. Shows how to create your first resource.

Setting up an entry point

To achieve this, you will first need to redirect all traffic to one file. This can be done using an .htaccess file.

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On

    RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteBase /

    # remove the trailing slash
    RewriteRule ^(.+)/$ $1 [R=301,L]

    RewriteRule ^ index.php [L]

Once you have that in place, all traffic will be redirected to your entry file index.php. The following is a simple index.php file that we will use in this tutorial.


use BeRest\API\Application;

define('ROOTDIR', __DIR__ . '/..');

require_once ROOTDIR . '/vendors/composer/autoload.php';

$app = new Application();

$app->registerComponent('errors', new \BeRest\API\Components\Errors());
$app->registerComponent('configs', new \BeRest\GettingStarted\Components\DevConfigs());
$app->registerComponent('services', new \BeRest\API\Components\Services());
$app->registerComponent('middlewares', new \BeRest\API\Components\Middlewares());


$app->registerFactory('controllers', new \BeRest\API\Controllers\Factory('BeRest\\GettingStarted'));
$app->registerFactory('managers', new \BeRest\API\Managers\Factory('BeRest\\GettingStarted'));



The first step is to configure the framework and to include it in our entry file. Then, we create a new instance of an application. We then register the different components and initialize the application. Then, we register the resource and we run the application.

Base Classes

In this tutorial, we will override 2 base classes from the API, Configs.php with our own configurations.

Let's create a new file called DevConfigs.php and let's add the following code.


namespace BeRest\GettingStarted;

use BeRest\API\Application;
use BeRest\API\Components\Configs;

class DevConfigs extends Configs
    public function preRun(Application $app)

        $app[''] = '';
        $app['redis.port'] = '6379';

        $app[''] = '';
        $app['mysql.port'] = '3306';
        $app['mysql.user'] = 'root';
        $app['mysql.password'] = '123456';
        $app['mysql.db'] = 'berest';

Adding our first resource

The first step is to add our model which is based on the table in our database. The following fields are mandatory for each model and are automatically added when extending the Base model class, id, dateCreated and dateUpdated.


namespace BeRest\GettingStarted\Models;

use BeRest\API\Models\Base;

class Resource extends Base
    public $name;

Then, we define the manager which is responsible for accessing the data and controlling the cache. The constant MODEL_NAME is a reference to the model we just defined. The constant CLASS_NAME is the name of the table in our database.


namespace BeRest\GettingStarted\Managers;

use BeRest\API\Managers\Base;

class Resources extends Base
    const MODEL_NAME = 'Resource';
    const CLASS_NAME = 'resources';

Finally, we define the controller which is responsible for defining the interface to the external world. Since in this tutorial, we don't have user authentication, we will redefine the routes to all be public.


namespace BeRest\GettingStarted\Controllers;

use BeRest\API\Controllers\Base;

class Resources extends Base
    public static $routes = [
        'get_' => ['function' => 'getAll'],
        'get_/{id}' => ['function' => 'getOne'],
        'post_' => ['function' => 'create'],
        'put_/{id}' => ['function' => 'update'],
        'delete_/{id}' => ['function' => 'delete']

Using the API

At this point, you now have a working API with one simple resource. Let's make some API call using Curl.

Let's start by creating a resource. This call will actually return the newly created resource if successful.

curl -X POST -H "Content-Type: application/json" -d '{"name":"Temp1"}'

Listing all the resources will return a list of resources along with pagination information.


We can also use filtering and sorting when retrieving the list of resources as well as pagination settings.

curl -g[name]=like(Temp)&sort[name]=asc&page=1&perPage=30

We can then retrieve the information about one specific resource.


We can also modify a resource.

curl -X PUT -H "Content-Type: application/json" -d '{"name":"TempName"}'

Finally, we can delete a resource.

curl -X DELETE