#SlimStatic
Slim PHP static proxy library.
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.
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\\*');
The following static proxies are available:
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 |
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();
Sugar for Slim config, using the following methods:
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);
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 () {...});
Proxy to the Slim\Http\Request instance with an additional method:
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');
Proxy to the Slim\Log instance.
Log::info('My info');
Log::debug('Degug info');
Proxy to the Slim\Http\Request instance.
$path = Request::getPath();
$xhr = Request::isAjax();
Proxy to the Slim\Http\Response instance.
Response::redirect('/success');
Response::headers->set('Content-Type', 'application/json');
Sugar for the following Slim instance route-mapping methods:
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.
Proxy to the Slim\View instance
View::display('hello.html');
$output = View::render('world.html');
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.
SlimStatic is licensed under the MIT License - see the LICENSE
file for details