sugiphp/sugi

SugiPHP Micro Framework

v0.0.1 2014-09-12 13:12 UTC

This package is not auto-updated.

Last update: 2021-10-10 16:38:36 UTC


README

Build Status

Sugi is a micro framework using SugiPHP Components as well as some other components. It works as a Dependency Injection Container and it is very flexible.

Hello World! example

<?php
$app = new \SugiPHP\Sugi\App();
$app->route("HelloWorld", "/hello", function () {
    echo "Hello World!";
});
$app->run();
?>

Installation

composer require sugiphp/sugi:dev-master

Usage

Defined paths

<?php

use SugiPHP\Sugi\App;

// Instantiate SugiPHP Application:
$app = new App(["base_path" => dirname(__DIR__) . "/"]);

// Or use Singleton pattern:
$app = App::getInstance();
?>

PSR-3 Logger

The App is using SugiPHP\Logger by default, which is PSR-3 compliant. To use logger you can use one lazy method: log(string $level, string $message, array $context = []), or use the instance app["logger"] to access methods given by the specification:

<?php
    $app->log("debug", "Debug message");
    $app->log("info", "user input was", $_POST);

    $app["logger"]->error("my error message");
    $app["logger"]->setLevel("warning");
    $app["logger"]->info("this will not be logged");
?>

PSR-7 Requests

The App is PSR-7 compatible and is using zendframework/zend-diactoros internally. You can plug any other PSR-7 compatible library for ServerRequest (instance of \Psr\Http\Message\ServerRequestInterface.)

<?php

$app["request"] = new \Your\ServerRequest();

?>

The URI is an instance of \Psr\Http\Message\UriInterface, so you can use:

<?php

$app["uri"]->getHost();
$app["uri"]->getPath();

?>

and all other PSR-7 UriInterface methods. Note that manipulating an $app["uri"] will not change it's value:

<?php

echo $app["uri"]->getPath(); // "/"
echo $app["uri"]->withPath("/foo/bar")->getPath(); // "/foo/bar"
echo $app["uri"]->getPath(); // "/"

// to override it:
$app["uri"] = $app["uri"]->withPath("/foo");
echo $app["uri"]->getPath(); // "/foo"

?>

Cache

The default cache is a virtual cache that is only available till the end of the script. All values can be set and get with one method cache()

<?php

$app->cache("key", "value");
echo $app->cache("key"); // returns "value"

// to access all methods for the cache you can use:
$app["cache"]->has("key"); // returns TRUE

?>

By default Sugi is using SugiPHP Cache. To set up Sugi to use memcached server you can use a configuration file /path/to/config/cache.php:

return [
    "store" => "memcached",
    "host" => "127.0.0.1",
    "port" => 11211, 
    "prefix" => "myprefix",
]

See config file example