Slim PHP static proxy library

Overview

#SlimStatic

Scrutinizer Code Quality Build Status

Slim PHP static proxy library.

Contents

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.

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\\*');

API

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

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

Config

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

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 () {...});

Input

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');

Log

Proxy to the Slim\Log instance.

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

Request

Proxy to the Slim\Http\Request instance.

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

Response

Proxy to the Slim\Http\Response instance.

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

Route

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.

View

Proxy to the Slim\View instance

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

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.

License

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

You might also like...
CORS Middleware for PHP Slim Framework

CorsSlim Cross-origin resource sharing (CORS) Middleware for PHP Slim Framework. Usage Composer Autoloader Install with Composer Update your composer.

PHP slim framework middleware to minify HTML output

slim-minify Slim middleware to minify HTML output generated by the slim PHP framework. It removes whitespaces, empty lines, tabs beetween html-tags an

Slim Framework - Prerequisite Checker
Slim Framework - Prerequisite Checker

Slim Framework - Server Configuration Checker Upload the file check.php to your webserver Browse to the file: https://example.com/check.php Check the

REST APIs using Slim framework. Implemented all CRUD operations on the MySql database
REST APIs using Slim framework. Implemented all CRUD operations on the MySql database

PHP REST API using slim framework and CRUD operations 🖐 Hi there, this is a simple REST API built using the Slim framework. And this is for the folks

Slim 3 skeleton working with Google App Engine include cron configuration.

Slim3 GAE Skeleton Slim 3 skeleton working with Google App Engine include cron configuration. Demo https://slim3-gae-skeleton.appspot.com/health_check

Slim Framework skeleton application with MVC Schema

Slim Framework skeleton application with MVC Schema

A curated list of awesome tutorials and other resources for the Slim micro framework

Awesome Slim A curated list of awesome tutorials and other resources for the Slim micro framework Table of Contents Essentials Tutorials Packages and

Slim Framework 3 Skeleton Application + PagSeguro Lib

Slim Framework 3 Skeleton Application + PagSeguro Lib Aplicação simples para geração do Token para pagamentos no PagSeguro (método transparente) e env

My personal blog developed on the Slim Framework

nesbot.com I am making the source code of my personal site available publicly in case it helps anybody. It's developed using the Slim Framework. I am

Comments
  • Added support for Slim 3

    Added support for Slim 3

    I tested the Container class as that was the thing I'm using this library the most for, the Config class isn't working yet as static::$slim->config($key) will result in a method not found.

    opened by KeizerDev 4
  • No Updates?

    No Updates?

    The static interface is cool! Nicer than Slim original API. I use this on some projects. I don't understand PHP well, so i just hope this library will have some update soon ^^

    Cheers!

    opened by explorewp 1
  • View::display not working

    View::display not working

    I followed the sample, with View::display(), and it only throws the following error:

    View cannot render `hello.html` because the template doesn't exist.
    File: <mylocalpath>\ vendor\slim\slim\Slim\View.php
    

    I've tried setting templates.path, relative paths, ../../ , etc, and it never finds the templates path used $app->config(), Config::set() and App::config(), but it doesn't fix the templates path.

    opened by vhanla 0
Owner
John Stevenson
John Stevenson
XStatic is a PHP library for enabling static proxy interfaces

XStatic is a PHP library for enabling static proxy interfaces—similar to Laravel 4+ "Facades"—but with any PHP project. XStatic was created by Jeremy Lindblom.

Jeremy Lindblom 104 Dec 8, 2022
"Static" interface for various Slim features

SlimFacades SlimFacades is a collection of facades for Slim PHP microframework, providing simple "static" interface for various Slim features. For exa

its 74 May 12, 2022
Socks5 proxy server written in Swoole PHP

php-socks This is a Socks5 proxy server implementation built with PHP & Swoole. To start the proxy server, clone this repo, run composer install to in

Nazmul Alam 3 Jan 23, 2022
This repository contains a library of optional middleware for your Slim Framework application

Slim Framework Middleware This repository contains a library of optional middleware for your Slim Framework application. How to Install Update your co

Slim Framework 47 Nov 7, 2022
This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library

This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library. It supports minification and caching, also via Asseti

Gerard Sychay 22 Mar 31, 2020
A static analysis engine

A static analysis engine... Usage: bin/tuli analyze file1 file2 path Installation Install it as a composer dependency!!! $ composer require ircmaxell

Anthony Ferrara 171 Jan 31, 2022
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Slim Framework Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. Installation It's recommended

Slim Framework 11.5k Jan 4, 2023
A Slim PHP MVC framework built just for fun!

Aura Framework A Slim PHP MVC framework built just for fun! en: Note: This repository only contains the core code of the Aura framework. If you want t

Murilo Magalhães Barreto 2 Dec 16, 2021
The Slim PHP micro framework paired with Laravel's Illuminate Database toolkit.

Slim & Eloquent The Slim PHP micro framework paired with Laravel's Illuminate Database toolkit. Getting started # Download composer curl -s https://ge

Kyle Ladd 111 Jul 23, 2022
PHP boilerplate for quick start projects using Slim Framework and Eloquent.

Lassi PHP boilerplate for quick projects using Slim Framework and Eloquent database. Lassi is a small PHP boilerplate to use Slim Framework with Eloqu

Jabran Rafique⚡️ 59 Jul 14, 2022