このスケルトンアプリケーションを使用して、新しいSlim Framework 3アプリケーション(Slim 3.12でテスト)の迅速なセットアップと作業を開始します。このアプリケーションは、HTTPおよびコマンドラインリクエストを処理します。このアプリケーションには、いくつかのサービスプロバイダーとセッションミドルウェアが箱から出されます。コンテナの解像度と自動配線のサポート。
サービスプロバイダーを削除するには、config/app.phpファイルにコメントし、composer.jsonのcomposerを更新します。
利用可能なサービスプロバイダー:
利用可能なミドルウェア:
新しいスリムフレームワークアプリケーションをインストールするディレクトリからこのコマンドを実行します。
php composer.phar create-project jupitern/slim3-skeleton [my-app-name]
[my-app-name]新しいアプリケーションの目的のディレクトリ名に置き換えます。あなたはしたい:
public/ディレクトリに向けます。storage/はWeb writableであることを確認してください。$ cd [my-app-name]public$ php -S localhost:8080または$ composer serveapp :アプリケーションコード(モデル、コントローラー、CLIコマンド、ハンドラー、ミドルウェア、サービスプロバイダーなど)config :DB、メール、ルートなどの構成ファイル...lib :UTILS、ビジネスロジック、フレームワーク拡張などの他のプロジェクトクラスstorage :ログファイル、キャッシュファイル、およびLess Less、SASS、JavaScriptなどの未加工の非コンパイルされた資産。public :パブリックディレクトリには、 index.phpファイル、画像、javascript、cssなどのアセットが含まれていますviews :ビューテンプレートファイル。vendor :作曲家の依存関係アプリクラスには、次のルートリゾルバー方法があります。
Webサイトとバックエンドフォルダーの2つのルートを定義する例:
use Psr Http Message ServerRequestInterface as Request ;
use Psr Http Message ResponseInterface as Response ;
// simple route example
$ app -> get ( ' /welcome/{name} ' , function ( Request $ request , Response $ response , $ args ) {
$ name = $ request -> getAttribute ( ' name ' );
$ response -> getBody ()-> write ( " Hello, $ name " );
return $ response ;
});
// example route to resolve request to uri '/' to AppHttpSiteWelcome::index
$ app -> any ( ' / ' , function ( $ request , $ response , $ args ) use ( $ app ) {
return $ app -> resolveRoute ([ App Http Welcome::class, " index " ], $ args );
});
// example calling http://localhost:8080/index.php/test/nuno with the route bellow
// injects the :name param value into the method $name parameter
// Other parameters in the method will be searched in the container by classname or automatically resolved
// in this example the resolveRoute method will create a user instance and inject it in the controller method
$ app -> any ( ' /test[/{name}] ' , function ( $ request , $ response , $ args ) use ( $ app ) {
return $ app -> resolveRoute ([ App Http Welcome::class, " method " ], $ args );
});
namespace App Http ;
use Jupitern Slim3 App Http Controller ;
class Welcome extends Controller
{
public function method ( $ name , App Model User $ user )
{
return get_class ( $ user ). " <br/>name = { $ name }" ;
}
}新しいコマンドを作成する方法:
例:
コマンドクラス:
namespace App Console ;
class Test extends Command
{
public function method ( $ a , $ b = ' foobar ' )
{
return
"n Entered console command with params: n" .
" a= { $ a }n" .
" b= { $ b }n" ;
}
}クラスを実行:コマンドラインからメソッド:
// since param "b" is optional you can use one of the following commands
> php cli.php Test method a=foo b=bar
> php cli.php Test method a=fooアプリケーションインスタンスを取得します
$ app = Lib Framework App:: instance ();
// or simpler using a helper function
$ app = app ();デバッグヘルパー関数を使用して、変数、配列、またはオブジェクトをデバッグ
debug ([ ' a ' , ' b ' , ' c ' ]);
// or debug and exit passing true as second param
debug ([ ' a ' , ' b ' , ' c ' ], true );Laravel Eloquentサービスプロバイダーを使用してDBのユーザーを読む
$ user = App Model User:: find ( 1 );
echo $ user -> Name ;構成ファイルに「メール」という名前のphpmailerサービスプロバイダーサービスを使用して電子メールを送信する
/* @var $mail PHPMailerPHPMailerPHPMailer */
$ mail = app ()-> resolve ( ' mail ' );
$ mail -> addAddress ( ' [email protected] ' );
$ mail -> Subject = " test " ;
$ mail -> Body = " <b>test body</b> " ;
$ mail -> AltBody = " alt body " ;
$ mail -> send ();構成ファイルに「fs_local」という名前のフライシステムサービスプロバイダーのディレクトリコンテンツをリストします
$ filesystem = app ()-> resolve ( ' fs_local ' );
$ contents = $ filesystem -> listContents ( STORAGE_PATH , true );
var_dump ( $ contents );セッションヘルパークラスを使用して、セッションから書いて読み取ります
// save user info in session
Jupitern Slim3 Utils Session:: set ( ' user ' , [ ' id ' => ' 1 ' ]);
// get user info from session
$ uservar = Jupitern Slim3 Utils Session:: get ( ' user ' );
var_dump ( $ uservar );構成ファイルで「Redis」という名前のRedisサービスプロバイダーを使用してキャッシュから書き込み読みます
/** @var JupiternSlim3UtilsRedis $cache */
$ cache = app ()-> resolve ( ' redis ' );
$ cache -> set ( " cacheKey " , " some test value " );
echo $ cache -> get ( " cacheKey " );v3.0
v2.6
v2.5
Jupitern/Slim3-SkeletonはMITライセンスの下でリリースされます。