Home

Awesome

#SlimStatic

Scrutinizer Code Quality Build Status

Slim PHP static proxy library.

Contents

<a name="About"></a>

About

SlimStatic provides a simple static interface to various features in the Slim micro framework. Turn this:

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

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

$app->run();

into this:

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

App::run();

This library is based on Slim-Facades from Miroslav Rigler, but uses Statical to provide the static proxy interface.

<a name="Usage"></a>

Usage

Install via composer

composer require statical/slim-static

Create your Slim app and boot SlimStatic:

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

$app = new Slim();

SlimStatic::boot($app);

Now you can start using the static proxies listed below. In addition there is a proxy to Statical itself, aliased as Statical and available in any namespace, so you can easily use the library to add your own proxies (see Customizing) or define namespaces.

If your app is namespaced you can avoid syntax like \App::method or use statements by employing the namespacing feature:

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

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

<a name="Api"></a>

API

The following static proxies are available:

Statical AliasProxy
Appto Slim instance
Configcalling the Slim config method
Containerto Slim container instance
Inputto Slim\Http\Request instance
Logto Slim\Log instance
Requestto Slim\Http\Request instance
Responseto Slim\Http\Response instance
Routecalling Slim route-matching methods
Viewto Slim\View instance

<a name="App"></a>

App

Proxy to the Slim instance. Note that you cannot use the built-in resource locator statically, because App::foo = 'bar' is not a method call. Use the Container proxy instead.

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

<a name="Config"></a>

Config

Sugar for Slim config, using the following methods:

$debug = Config::get('debug');
Config::set('log.enable', true);

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

<a name="Container"></a>

Container

Proxy to the Slim container instance. Use this to access the built-in resource locator.

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

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

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

<a name="Input"></a>

Input

Proxy to the Slim\Http\Request instance with an additional method:

$avatar = Input::file('avatar');
$username = Input::get('username', 'default');
$password = Input::post('password');

<a name="Log"></a>

Log

Proxy to the Slim\Log instance.

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

<a name="Request"></a>

Request

Proxy to the Slim\Http\Request instance.

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

<a name="Response"></a>

Response

Proxy to the Slim\Http\Response instance.

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

<a name="Route"></a>

Route

Sugar for the following Slim instance route-mapping methods:

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

Note that because these methods call the Slim instance you can also invoke them with App::get, App::post etc.

<a name="View"></a>

View

Proxy to the Slim\View instance

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

<a name="Custom"></a>

Customizing

Since Statical is already loaded, you can use it to create your own static proxies. Let's take a PaymentService class as an example, that you want to alias as Payment.

The first step is to create a proxy class that extends the Statical\BaseProxy class. It is normally empty and you can name it whatever you wish:

class PaymentProxy extends \Statical\BaseProxy {}

You must then register this with Statical, using addProxyInstance if you use a class instance, or addProxyService if you want to use the Slim container. Using a class instance:

# 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();

Using the Slim container:

# 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();

Note that for namespaced code, the namespace must be included in the $proxy param.

<a name="License"></a>

License

SlimStatic is licensed under the MIT License - see the LICENSE file for details