statical/slim-static

Slim框架的静态代理实现

v1.0.0 2014-11-13 22:27 UTC

This package is auto-updated.

Last update: 2024-09-05 18:18:16 UTC


README

#SlimStatic

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 statical/slim-static

创建你的Slim应用程序并启动SlimStatic

use Slim\Slim;
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已经加载,你可以使用它来创建自己的静态代理。以一个名为PaymentService的类为例,你想要将其别名为Payment

第一步是创建一个扩展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();

使用Slim容器

# 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 文件