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.
<?php
$app = new \SugiPHP\Sugi\App();
$app->route("HelloWorld", "/hello", function () {
echo "Hello World!";
});
$app->run();
?>
composer require sugiphp/sugi:dev-master
<?php
use SugiPHP\Sugi\App;
// Instantiate SugiPHP Application:
$app = new App(["base_path" => dirname(__DIR__) . "/"]);
// Or use Singleton pattern:
$app = App::getInstance();
?>
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");
?>
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"
?>
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