keizerdev/slim-static

Slim框架的静态代理实现

v1.0.1 2020-05-30 12:59 UTC

This package is auto-updated.

Last update: 2024-09-29 05:55:42 UTC


README

这是一个带有新包的运行分支

Scrutinizer Code Quality Build Status

Slim PHP静态代理库。

内容

关于

SlimStatic为Slim微框架中的各种功能提供了一个简单的静态接口。将此

$app->get('/hello-world', function()
{
	$app = Slim::getInstance();

	$app->view()->display('hello.html', array(
        'name' => $app->request()->get('name', 'world')
    ));
});

$app->run();

转换为

Route::get('/hello-world', function()
{
	View::display('hello.html', array(
        'name' => Input::get('name', 'world')
    ));
});

App::run();

这个库基于Miroslav Rigler的Slim-Facades,但使用Statical提供静态代理接口。

用法

通过composer安装

composer require keizerdev/slim-static

创建您的Slim应用并启动SlimStatic

use Slim\App;
use Statical\SlimStatic\SlimStatic;

$app = new Slim();

SlimStatic::boot($app);

现在您可以使用以下静态代理开始使用。此外,还有一个到Statical本身的代理,别名为Statical,在任何命名空间中都可使用,因此您可以轻松地使用库添加自己的代理(请参阅自定义)或定义命名空间。

如果您的应用是命名空间化的,您可以通过使用命名空间功能来避免使用如\App::methoduse语句的语法

# Allow any registered proxy to be called anywhere in the `App\Name` namespace

Statical::addNamespace('*', 'App\\Name\\*');

API

以下为可用的静态代理

App

到Slim实例的代理。请注意,您不能静态地使用内置的资源定位器,因为App::foo = 'bar'不是一个方法调用。请使用Container代理。

App::expires('+1 week');
App::halt();

Config

为Slim配置提供的糖,使用以下方法

  • get($key) - 返回$app->config($key)的值
  • set($key, $value = null) - 调用$app->config($key, $value)
$debug = Config::get('debug');
Config::set('log.enable', true);

# Note that you could also use:
$debug = App::config('debug');
App::config('log.enable', true);

Container

到Slim容器实例的代理。使用此方法访问内置的资源定位器。

# $app->foo = 'bar'
Container::set('foo', 'bar');

# $bar = $app->foo
$bar = Container::get('foo');

Container::singleton('log', function () {...});
$rawClosure = Container::protect(function () {...});

Input

到Slim\Http\Request实例的代理,并额外提供以下方法

  • file($name) - 返回$_FILES[$name],如果请求中没有发送该文件则返回null
$avatar = Input::file('avatar');
$username = Input::get('username', 'default');
$password = Input::post('password');

Log

到Slim\Log实例的代理。

Log::info('My info');
Log::debug('Degug info');

Request

到Slim\Http\Request实例的代理。

$path = Request::getPath();
$xhr = Request::isAjax();

Response

到Slim\Http\Response实例的代理。

Response::redirect('/success');
Response::headers->set('Content-Type', 'application/json');

Route

为以下Slim实例路由映射方法提供的糖

  • mapgetpostputpatchdeleteoptionsgroupanyurlFor
Route::get('/users/:id', function ($id) {...});
Route::post('/users',  function () {...});
Route::urlFor('admin');

请注意,因为这些方法调用Slim实例,所以您也可以使用App::getApp::post等调用它们。

View

到Slim\View实例的代理

View::display('hello.html');
$output = View::render('world.html');

自定义

由于Statical已经被加载,您可以使用它来创建自己的静态代理。让我们以一个您希望别名为PaymentPaymentService类为例。

第一步是创建一个扩展Statical\BaseProxy类的代理类。通常它是空的,您可以给它起任何名字

class PaymentProxy extends \Statical\BaseProxy {}

然后您必须将其注册到Statical,如果您使用类实例,请使用addProxyInstance;如果您想使用Slim容器,请使用addProxyService

# create our PaymentService class
$instance = new \PaymentService();

$alias = 'Payment';             # The static alias to call
$proxy = 'PaymentProxy';        # The proxy class you just created

Statical::addProxyInstance($alias, $proxy, $instance);

# Now we can call PaymentService methods via the static alias Payment
Payment::process();

使用类实例

# Register our service with Slim's DI container
Container::set('payment', function () {
    return new \PaymentService();
});


$alias = 'Payment';             # The static alias to call
$proxy = 'PaymentProxy';        # The proxy class you just created
$id = 'payment';                # The id of our service in the Slim container

Statical::addProxyService($alias, $proxy, Container::getInstance(), $id);

# Now we can call PaymentService methods via the static alias Payment
Payment::process();

请注意,对于命名空间化的代码,必须在$proxy参数中包含命名空间。

许可证

SlimStatic遵循MIT许可证 - 请参阅LICENSE文件以获取详细信息