php express
Version 2.0.0
Micro-quadro PHP inspirado pelo Express.js
| Construir | Páginas do Github | Estável | Licença |
|---|---|---|---|
ext-json )Se o Composer ainda não estiver instalado no seu sistema, você poderá instalá -lo usando esta linha de comando:
$ curl -sS https://getcomposer.org/installer | php
Em seguida, adicione o seguinte requer entrada ao arquivo composer.json na raiz do seu projeto.
{
"require" : {
"riverside/php-express" : " ^2.0 "
}
}Por fim, use o Composer para instalar o PHP-Express e suas dependências:
$ 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 ' );
});| Método | Descrição |
|---|---|
| $ res-> end () | Encerrar o processo de resposta. |
| $ res-> json () | Envie uma resposta JSON. |
| $ res-> redirecion () | Redirecionar um pedido. |
| $ res-> render () | Renderizar um modelo de visualização. |
| $ res-> send () | Envie uma resposta de vários tipos. |
| $ res-> SendStatus () | Defina o código de status da resposta e envie sua representação de string como o corpo da resposta. |
<?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 ' , ' * ' );
});