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许可下发布。