SlimStatic
, (*1)
Slim PHP static proxy library., (*2)
Contents
, (*3)
About
SlimStatic provides a simple static interface to various features in the Slim
micro framework. Turn this:, (*4)
$app->get('/hello-world', function()
{
$app = Slim::getInstance();
$app->view()->display('hello.html', array(
'name' => $app->request()->get('name', 'world')
));
});
$app->run();
into this:, (*5)
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., (*6)
, (*7)
Usage
Install via composer, (*8)
composer require statical/slim-static
Create your Slim app and boot SlimStatic:, (*9)
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., (*10)
If your app is namespaced you can avoid syntax like \App::method or use statements
by employing the namespacing feature:, (*11)
# Allow any registered proxy to be called anywhere in the `App\Name` namespace
Statical::addNamespace('*', 'App\\Name\\*');
, (*12)
API
The following static proxies are available:, (*13)
| Statical Alias |
Proxy |
| App |
to Slim instance |
| Config |
calling the Slim config method |
| Container |
to Slim container instance |
| Input |
to Slim\Http\Request instance |
| Log |
to Slim\Log instance |
| Request |
to Slim\Http\Request instance |
| Response |
to Slim\Http\Response instance |
| Route |
calling Slim route-matching methods |
| View |
to Slim\View instance |
, (*14)
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., (*15)
App::expires('+1 week');
App::halt();
, (*16)
Config
Sugar for Slim config, using the following methods:, (*17)
-
get($key) - returns value of $app->config($key)
-
set($key, $value = null) - calls $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);
, (*18)
Container
Proxy to the Slim container instance. Use this to access the built-in resource locator., (*19)
# $app->foo = 'bar'
Container::set('foo', 'bar');
# $bar = $app->foo
$bar = Container::get('foo');
Container::singleton('log', function () {...});
$rawClosure = Container::protect(function () {...});
, (*20)
Proxy to the Slim\Http\Request instance with an additional method:, (*21)
-
file($name) - returns $_FILES[$name], or null if the file was not sent in the request
$avatar = Input::file('avatar');
$username = Input::get('username', 'default');
$password = Input::post('password');
, (*22)
Log
Proxy to the Slim\Log instance., (*23)
Log::info('My info');
Log::debug('Degug info');
, (*24)
Request
Proxy to the Slim\Http\Request instance., (*25)
$path = Request::getPath();
$xhr = Request::isAjax();
, (*26)
Response
Proxy to the Slim\Http\Response instance., (*27)
Response::redirect('/success');
Response::headers->set('Content-Type', 'application/json');
, (*28)
Route
Sugar for the following Slim instance route-mapping methods:, (*29)
-
map, get, post, put, patch, delete, options, group, any, urlFor
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., (*30)
, (*31)
View
Proxy to the Slim\View instance, (*32)
View::display('hello.html');
$output = View::render('world.html');
, (*33)
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., (*34)
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:, (*35)
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:, (*36)
# 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:, (*37)
# 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., (*38)
, (*39)
License
SlimStatic is licensed under the MIT License - see the LICENSE file for details, (*40)