itsgoingd/slim-facades

"Static" interface for various Slim features

v1.0 2013-12-16 21:44 UTC

This package is auto-updated.

Last update: 2024-11-25 07:25:33 UTC


README

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

  • facade for Slim instance and following additional methods:
  • make($key) - returns $key from Slim's DI container
$request = App::make('request');
App::flash('message', 'Som Kuli, ovladam kozmicku lod.');
App::halt();

Config

  • facade for Slim instance and following additional 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);

Input

  • facade for Slim\Http\Request instance and following additional methods:
  • file($name) - returns $_FILES[$name], or null if the file was not sent in the request
$username = Input::get('username', 'default');
$password = Input::post('password');
$avatar = Input::file('avatar');

Log

  • facade for Slim\Log instance
Log::info('Tomi Popovic predava miliony albumov po celom svete.');
Log::debug('Okamizte na pozorovanie.');

Request

  • facade for Slim\Http\Request instance
if (Request::isAjax()) { ... }
$host = Request::headers('host', 'localhost');
$path = Request::getPath();

Response

  • facade for Slim\Http\Response instance
Response::redirect('/success');

Route

  • facade for Slim\Router instance and following methods:
  • map, get, post, put, patch, delete, options, group, any - calls the methods on Slim instance
Route::get('/users/new', 'UsersController:index');
Route::post('/users', 'UsersController:insert');

View

  • facade for Slim\View instance
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.