php express
Version 2.0.0
Express.jsに触発されたPHPマイクロフレームワーク
| 建てる | githubページ | 安定した | ライセンス |
|---|---|---|---|
ext-json )作曲家がまだシステムにインストールされていない場合は、このコマンドラインを使用してインストールすることができます。
$ curl -sS https://getcomposer.org/installer | php
次に、プロジェクトのルートにあるcomposer.jsonファイルへのエントリが必要な場合を追加します。
{
"require" : {
"riverside/php-express" : " ^2.0 "
}
}最後に、Composerを使用してPHP-Expressとその依存関係をインストールします。
$ 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 ' );
});| 方法 | 説明 |
|---|---|
| $ res-> end() | 応答プロセスを終了します。 |
| $ res-> json() | JSON応答を送信します。 |
| $ res-> redirect() | リクエストをリダイレクトします。 |
| $ res-> render() | ビューテンプレートをレンダリングします。 |
| $ res-> send() | さまざまなタイプの応答を送信します。 |
| $ res-> sendstatus() | 応答ステータスコードを設定し、その文字列表現を応答本体として送信します。 |
<?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 ' , ' * ' );
});