php express
Version 2.0.0
PHP Micro-Framework inspiriert von Express.js
| Bauen | Github -Seiten | Stabil | Lizenz |
|---|---|---|---|
ext-json )Wenn der Komponist noch nicht auf Ihrem System installiert ist, können Sie es mit dieser Befehlszeile installieren:
$ curl -sS https://getcomposer.org/installer | php
Fügen Sie anschließend die folgende Erfordernisse der Datei composer.json im Stamm Ihres Projekts hinzu.
{
"require" : {
"riverside/php-express" : " ^2.0 "
}
}Verwenden Sie schließlich den Komponisten, um PHP-Express und seine Abhängigkeiten zu installieren:
$ php composer.phar install
<?php
$ app = new Riverside Express Application ();
$ app -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' hello world ' );
}); <?php
// GET method route
$ app -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' GET request to the homepage ' );
});
// POST method route
$ app -> post ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' POST request to the homepage ' );
}); <?php
$ app -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' root ' );
});
$ app -> get ( ' about ' , function ( $ req , $ res ) {
$ res -> send ( ' about ' );
});
$ app -> get ( ' random.text ' , function ( $ req , $ res ) {
$ res -> send ( ' random.text ' );
});| Verfahren | Beschreibung |
|---|---|
| $ res-> end () | Beenden Sie den Antwortprozess. |
| $ res-> json () | Senden Sie eine JSON -Antwort. |
| $ res-> redirect () | Eine Anfrage umleiten. |
| $ res-> render () | Eine Ansichtsvorlage rendern. |
| $ res-> send () | Senden Sie eine Antwort verschiedener Typen. |
| $ res-> sendStatus () | Legen Sie den Antwortstatuscode ein und senden Sie seine String -Darstellung als Antwortkörper. |
<?php
$ app -> route ( ' /book ' )
-> get ( function ( $ req , $ res ) {
$ res -> send ( ' Get a random book ' );
})
-> post ( function ( $ req , $ res ) {
$ res -> send ( ' Add a book ' );
})
-> put ( function ( $ req , $ res ) {
$ res -> send ( ' Update the book ' );
}); <?php
$ router = new Riverside Express Router ( $ app );
$ router -> param ( ' uuid ' , ' [a-fd]{8}-[a-fd]{4}-[a-fd]{4}-[a-fd]{4}-[a-fd]{12} ' );
$ router -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' Birds home page ' );
});
$ router -> get ( ' about ' , function ( $ req , $ res ) {
$ res -> send ( ' About birds ' );
});
$ router -> get ( ' ticket/:uuid/ ' , function ( $ req , $ res ) {
echo $ req -> params [ ' uuid ' ];
});
$ router -> run (); $ app -> use ( function ( $ req , $ res ) {
$ res -> header ( ' X-Frame-Options ' , ' DENY ' );
$ res -> header ( ' X-Powered-By ' , false );
});
$ app -> use ( ' /cors ' , function ( $ req , $ res ) {
$ res -> header ( ' Access-Control-Allow-Origin ' , ' * ' );
});