Phlyty is a PHP microframework written using Zend Framework 2 components. It’s goals are:
The features and API are roughly analagous to Slim Framework.
I recommend using Composer <https://getcomposer.org/>. Once you have composer, create the following composer.json file in your project:
{
"repositories": [
{
"type": "composer",
"url": "http://packages.zendframework.com/"
}
],
"minimum-stability": "dev",
"require": {
"phly/phlyty": "dev-master",
}
}
Then run php composer.phar install to install the library. This will ensure you retrieve Phlyty and all its dependencies.
The most basic “Hello World!” example looks something like this:
use Phlyty\App;
include 'vendor/autoload.php';
$app = new App();
$app->get('/', function ($app) {
echo "Hello, world!";
});
$app->run();
Assuming the above is in index.php, you can fire up the PHP 5.4 development web server to test it out:
php -S 127.0.0.1:8080
If you then visit http://localhost:8080/, you’ll see your “Hello, world!” text.
The main Phlyty\App class contains methods for each of the main HTTP request methods, and these all have the same API: method($route, $controller). They include:
All of them return a Phlyty\Route object, allowing you to further manipulate the instance – for example, to name the route, indicate what additional HTTP methods to respond to, or to access the controller or the composed ZF2 route object. (You can actually instantiate a ZF2 route object and pass that instead of a string for the route, which gives you more power and flexibility!)
$app->map('/', function ($app) {
echo "Hello, world!";
})->name('home'); // name the route
Alternately, you can use teh map() method. This simply creates the route, but does not assign it to a specific HTTP method. You would then use the via() method of the route object to assign it to one or more HTTP methods:
$app->map('/', function ($app) {
echo "Hello, world!";
})->via('get', 'post')->name('home'); // name the route, and have it respond
// to both GET and POST requests
By default, if you pass a string as the $route argument, Phlyty\App will create a ZF2 Segment route; you can read up on those in the ZF2 manual. In such routes, a string preceded by a colon will indicate a named variable to capture: /resource/:id would capture an “id” value. You can have many named segments, and even optional segments.
Your controllers can be any PHP callable. In the examples, I use closures, but any callable is accepted. The callable will receive exactly one argument, the Phlyty\App instance.
From the App instance, you have the following helper methods available: