Coder Social home page Coder Social logo

slim-facades's Introduction

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

  • 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.

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.

slim-facades's People

Contributors

itsgoingd avatar jackmcdade avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.