PHP code example of zaek / framy

1. Go to this page and download the library: Download zaek/framy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zaek / framy example snippets



{
    $app = new \Zaek\Framy\App([
        'homeDir' => __DIR__,
        'routes' => [
            'GET /' => '/web/Index.php',
        ]
    ]);
    $app->handle();
    $app->response()->flush();

} catch (\Exception $e) {
    echo $e->getMessage();
}

\Zaek\Framy\App::setRouter(\Zaek\Framy\Router)

[
  '[METHOD[:RESPONSE_TYPE] ]URI' => TARGET
] 

new Router(new RoutePrefix('/api', [
    '/users' => '/users.php',
    '/settings' => '/v.php'
]));

// Is the same as

new Router([
    '/api/users' => '/users.php',
    '/api/settings' => '/settings.php'
]);

new Router([
    ...new RoutePrefix('/api', [
        '/settings' => '/settings.php',
        ...new RoutePrefix('/users', [
            'GET /' => '/users_list.php',
            ...new RoutePrefix('/auth', [
                'POST /login' => '/login.php',
                'POST /logout' => '/logout.php',
            ])
        ])
    ])),
    ...new RoutePrefix('/catalog', ['/' => '/catalog.php']),
    '/' => '/index.php'
]);

$routes = new RoutePrefix('/users', ['GET /' => '/users.php']);

$v1 = new RoutePrefix('/v1', ['GET /version' => '/v1.php', ...$routes]);
$v2 = new RoutePrefix('/v2', ['GET /version' => '/v2.php', ...$routes]);
$v3 = new RoutePrefix('/v3', ['GET /version' => '/v3.php', ...$routes, ...['GET /users/' => '/users_change.php']]);

$router = new Router(new RoutePrefix('/api', [
    ...$v1,
    ...$v2,
    ...$v3,
]));

// /api/v1/users => /users.php
// /api/v2/users => /users.php
// /api/v3/users => /users_changed.php

$proxyCallback = function($route) {
    return new CbFunction(function(App $app) use($route) {
        if($app->user()->getId() < 1) {
            throw new Exception('No auth', 401);
        }

        return new File($route);
    });
};

/// The request /api/user/data and /api/personal/data will be proxy by proxyCallback
///     and will throw an exception for non-authorized users

new Router(new RoutePrefix('/api', [
    '/data' => '/data.php',
    // It could be the proxy inside a group as a group inside the proxy 
    ... new RoutePrefix('/user', new RouteProxy($proxyCallback, [
        'GET /data' => '/user_data.php',
    ])),
    ... new RouteProxy($proxyCallback, new RoutePrefix('/personal', [
        'GET /data' => '/user_personal.php',
    ]))
]))

id = 10012019
login = 'framy'
email = '[email protected]'

[
  '/api/staticCall' => '/Web/Index.php',
  'GET:json|CLI /api/runCron' => '/Cli/Job.php',
  '/api/users/<userId:[\d]+>' => '/Web/User.php', // access to $userId from $this->request()->getQuery('userId')
  '/api/functionCall' => function(Controller $app) : Action {},
  '/api/methodCall' => ['MyController', 'MethodAction'], // Вызывается непосредственно для получения Action
  '/api/anotherFunction' => ['FunctionName'], // Вызывается непосредственно для получения Action
  '/api/actionCall' => new Zaek\Framy\Action\Action,
  '/api/absolutePathFileCall' => '@/var/www/index.html',
]

new Router(['REST /projects' => '/api/projects/']);

[
    'GET:json /projects' => '/api/projects/List.php',
    'POST:json /projects' => '/api/projects/Add.php',
    'GET:json /projects/<project_id:[\d]+>' => '/api/projects/Item.php',
    'PATCH:json /projects/<project_id:[\d]+>' => '/api/projects/Update.php',
    'DELETE:json /projects/<project_id:[\d]+>' => '/api/projects/Delete.php',    
]