slim3 skeleton
packages updated
使用此骨架應用程序快速設置並開始在新的Slim Framework 3應用程序上工作(用Slim 3.12測試)。此應用程序處理HTTP和命令行請求。該應用程序與一些服務提供商一起發貨,並開箱即用。支持集裝箱分辨率和自動接線。
要刪除服務提供商在config/app.php文件上評論,然後從composer.json中刪除它,請更新作曲家。
可用的服務提供商:
可用中間件:
從要安裝新的Slim Framework應用程序的目錄中運行此命令。
php composer.phar create-project jupitern/slim3-skeleton [my-app-name]
將[my-app-name]替換為您的新應用程序所需的目錄名稱。您將要:
public/目錄。storage/是網絡寫作。$ cd [my-app-name]public$ php -S localhost:8080或$ composer serveapp :應用程序代碼(模型,控制器,CLI命令,處理程序,中間件,服務提供商等)config :配置文件,例如數據庫,郵件,路由...lib :其他項目類,例如UTIL,業務邏輯和框架擴展storage :日誌文件,緩存文件和您的原始未編譯資產,例如Limes,Sass或JavaScript。public :公共目錄包含index.php文件,諸如圖像,JavaScript和CSS之類的資產views :視圖模板文件。vendor :作曲家依賴關係應用程序類具有:路由解析器方法:
為網站和後端文件夾定義兩條路線的示例:
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雄辯的服務提供商從數據庫中閱讀用戶
$ 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”的Flysystem服務提供商的目錄內容
$ filesystem = app ()-> resolve ( ' fs_local ' );
$ contents = $ filesystem -> listContents ( STORAGE_PATH , true );
var_dump ( $ contents );使用Session Helper類寫和閱讀
// 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服務提供商從CONPOIN文件中寫下和讀取名為“ 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許可下發布。