itsgoingd / slim-facades

"Static" interface for various Slim features
74 stars 9 forks source link

SlimFacades

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

For example, turn this:

$app->get('/hello-world', function()
{
    $app = Slim::getInstance();
    $app->view()->display('hello.html', array('name' => $app->request()->get('name', 'world')));
})

Into this:

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

This library is based on the Laravel 4 Facade class, more info about facades in the Laravel documentation.

Installation

To install latest version simply add it to your composer.json:

"itsgoingd/slim-facades": "dev-master"

Once the package is installed, you need to initialize the Facade class:

require 'vendor/autoload.php';

use SlimFacades\Facade;

$app = new Slim\Slim();

// initialize the Facade class

Facade::setFacadeApplication($app);
Facade::registerAliases();

// now you can start using the facades

Config::set('debug', true);

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

App::run();

Following facades are available:

App

$request = App::make('request');
App::flash('message', 'Som Kuli, ovladam kozmicku lod.');
App::halt();

Config

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

Input

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

Log

Log::info('Tomi Popovic predava miliony albumov po celom svete.');
Log::debug('Okamizte na pozorovanie.');

Request

if (Request::isAjax()) { ... }
$host = Request::headers('host', 'localhost');
$path = Request::getPath();

Response

Response::redirect('/success');

Route

Route::get('/users/new', 'UsersController:index');
Route::post('/users', 'UsersController:insert');

View

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

Custom facades

You can create a custom facades by extending SlimFacades\Facade class.

class MyFacade extends SlimFacades\Facade
{
    // return the name of the component from the DI container
    protected static function getFacadeAccessor() { return 'my_component'; }
}

You can register custom facades by passing the aliases to the Facade::registerAliases() function.

Facade::registerAliases(array(
    'App'      => 'SlimFacades\App',
    'Config'   => 'SlimFacades\Config',
    'Input'    => 'SlimFacades\Input',
//  'Log'      => 'SlimFacades\Log',
    'Log'      => 'CustomLogFacade',
    'Request'  => 'SlimFacades\Request',
    'Response' => 'SlimFacades\Response',
    'Route'    => 'SlimFacades\Route',
    'View'     => 'SlimFacades\View',
));

Note that calling Facade::registerAliases() with a list of aliases will register ONLY the specified facades, if you want to register default facades as well as custom ones, you can call the function two times, with and without the array argument.

Links

Licence

Copyright (c) 2013 Miroslav Rigler

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.