Ejemplo n.º 1
0
<?php

//One Micro Framework - Hello World
//remember enable the .htacess in this folder
require_once 'src/OnePHP/one_framework.php';
/*
 * Remember remove this examples to avoid collisions in routes
 */
//load Micro Framework with debug enabled
$app = new \OnePHP\App();
$app->get('/', function () use($app) {
    //Action on the Root URL
    echo 'Hello world';
});
//test with slug in URL ( ':name' = '{name}' )
$app->get('/:name', function ($name) use($app) {
    echo "<h1> Hello <small> {$name} </small> </h1>";
});
//simple Json Response example
$app->get('/json/:name', function ($name) use($app) {
    return $app->JsonResponse(array('name' => $name));
});
$app->respond(function () use($app) {
    return $app->ResponseHTML('<p> This is a response with code 404. </p>', 404);
});
//Run
$app->listen();
Ejemplo n.º 2
0
{
    $response = array("success" => true);
    if ($token == '') {
        $response["success"] = false;
        $response["message"] = "INVALID_TOKEN";
    }
    return $response;
}
$app = new \OnePHP\App();
$app->get('/api', function () use($app) {
});
$app->get('/api/:action', function ($action) use($app) {
    // verifica o Token
    $auth = checkAuth($app->getRequest()->header("Authorization"));
    if (!$auth["success"]) {
        $app->JsonResponse($auth);
        return;
    }
    // define o nome da model, se não existir usa a base model
    $entity = ucfirst(load($action));
    // instancia a model, passando o nome da action (tabela) como parametro
    $model = new $entity($action);
    // chama o metodo
    try {
        $response = $model->getAll();
        // imprime a resposta em JSON
        $app->JsonResponse($response);
    } catch (Exception $e) {
        // imprime a resposta em JSON
        $app->JsonResponse('[' . $e->getMessage() . ']', 500);
    }