"Static" interface for various Slim features

Overview

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.

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.

Comments
  • These Are Not Facades

    These Are Not Facades

    Hey man -- these aren't "facades." They're static proxies to service locators. I'm guessing you got the term from Laravel; they misuse the term.

    opened by pmjones 24
  • View::render and View::display not rendering variable

    View::render and View::display not rendering variable

    Hi,

    I am trying to implement twig as slim view renderer,

    when I am using Slim-facade to render a view, variable is not passed to the view

    template.twig

    hello {{ name }}!
    

    the routes.php

    View::display('template.twig', array('name' => 'John'));
    //this only produce 'Hello !'
    
    $app->render('template.twig', array('name' => 'John'));
    //this produce 'Hello John!' perfectly
    

    I am using this code to initiate Slim, slim-facades, and twig

    require __DIR__.'/../../vendor/autoload.php';
    use SlimFacades\Facade;
    use Illuminate\Database\Capsule\Manager as Capsule;
    
    /**
     * Initialize Slim application
     */
    
    $app = new Slim\Slim(array(
        'view'              => new \Slim\Views\Twig(),
        'templates.path'    => __DIR__.'/../views'
    ));
    
    $view = $app->view();
    $view->parserOptions = array(
        'debug' => true,
        'cache' => __DIR__ . '/../storage/cache'
    );
    
    $view->parserExtensions = array(
        new \Slim\Views\TwigExtension(),
    );
    
    // initialize the Slim Facade class
    
    Facade::setFacadeApplication($app);
    Facade::registerAliases(array(
        'Model'     => 'Illuminate\Database\Eloquent\Model',
        'Sentry'    => 'Cartalyst\Sentry\Facades\Native\Sentry',
        'App'       => 'SlimFacades\App',
        'Config'    => 'SlimFacades\Config',
        'Input'     => 'SlimFacades\Input',
        'Log'       => 'SlimFacades\Log',
        'Request'   => 'SlimFacades\Request',
        'Response'  => 'SlimFacades\Response',
        'Route'     => 'SlimFacades\Route',
        'View'      => 'SlimFacades\View',
    ));
    
    
    opened by ikhsan017 4
  • Replace Laravel Facades with Statical library

    Replace Laravel Facades with Statical library

    I have been testing my static proxy library with a few frameworks (prior to publishing it on Packagist) and came across your excellent Slim Facades. It wired up very smoothly so I wondered if you would prefer to use it in place of your Laravel Facade implementation.

    I am not trying to push my library, so feel free to bin it and close this PR. Thanks for your time.

    opened by johnstevenson 2
  • Issues creating custom

    Issues creating custom "facade"

    I must be doing something wrong when attempting to create a custom facade. I have a SystemAlert class I'd like to create a Facade for and I have this.

    $app->container->singleton('system_alert', function () {
        return new SystemAlert();
    });
    
    class SystemAlertFacade extends SlimFacades\Facade
    {
        // return the name of the component from the DI container
        protected static function getFacadeAccessor() { return 'system_alert'; }
    }
    
    $config['aliases'] = array(
                   'Alert'  => 'SystemAlert',
                );
    SlimFacades\Facade::registerAliases($config['aliases']);
    

    From my understanding this is all you need to do and I should be able to use the SystemAlerts class like so:

    Alert::set(...);
    

    Unfortunately it doesn't work. However all the following work:

    $this->app->system_alert->set(); // inside a controller using DI $alert = new SystemAlert(); $alert = App:make('system_alert');

    In my SystemAlert class I have several properties. If I instantiate the SystemAlerts class I can call the methods and they can access the properties without issues. However when I attempt to use the facade I get errors stating that my properties do not exist. I'm at a loss as to what I'm doing wrong.

    Thanks!

    opened by eko3alpha 2
  • Route::resource

    Route::resource

    Hi,

    Thanks for creating great package to simplify Slim usage :)

    by the way, is there is any chance we can add Route::resource to the route facade?

    regards

    opened by ikhsan017 2
  • Register new aliases

    Register new aliases

    Hi, I've make some changes. Now you can declare new facades with Facade::registerAliases without redeclare all preexistent facades and if you want, for example, use custom Log class you can redeclare Log alias because with my changes if alias exist replace existent class with new class.

    opened by fdisotto 2
  • A simple example

    A simple example

    Hi, very nice for this work, I very like it :)

    But I think we need a more complete example to a properly use of these facades. An example like this:

    require_once __DIR__ . '/../vendor/autoload.php';
    
    use SlimFacades\Facade;
    
    $app = new \Slim\Slim();
    
    Facade::setFacadeApplication($app);
    Facade::registerAliases();
    
    Config::set('debug', true);
    
    Route::get('/hello/:name', function($name) {
        echo "Hello, $name";
    });
    
    App::run();
    

    If you like it please add it to your readme :) Thanks in advance.

    opened by fdisotto 2
Owner
its
webdev underground
its
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
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
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
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

Daniel Opitz 6 Aug 30, 2022
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

Hanoak 2 Jun 1, 2022
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
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

Jared Chu 2 May 10, 2018
Slim Framework skeleton application with MVC Schema

Slim Framework skeleton application with MVC Schema

JingwenTian 9 Apr 29, 2021
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
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

Sawyer Charles 466 Dec 8, 2022
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

Raí Siqueira 1 Feb 26, 2018
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

Brian Nesbitt 56 Sep 14, 2022
Slim 3 MVC Skeleton With Swoole

Slim 3 MVC Skeleton With Swoole ##Features Quickly setup and start working on a new Slim Framework 3 . Use the latest Slim 3 with the PHP-View templat

kcloze 53 Aug 17, 2022
Plates Template Integration for Slim micro framework 3

Plates Template Integration for Slim micro framework 3 Render your Slim 3 application views using Plates template engine. Install Via Composer $ compo

Projek XYZ 26 Feb 5, 2022
Juliangut Slim Framework Doctrine handler middleware

Juliangut Slim Framework Doctrine handler middleware Doctrine handler middleware for Slim Framework. Slim3 version Doctrine integration service for Sl

Julián Gutiérrez 6 Mar 23, 2021
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
Strict PSR-7 implementation used by the Slim Framework

Strict PSR-7 implementation used by the Slim Framework, but you may use it separately with any framework compatible with the PSR-7 standard.

Slim Framework 96 Nov 14, 2022
Slim Framework custom views

Slim Views This repository contains custom View classes for the template frameworks listed below. You can use any of these custom View classes by eith

Slim Framework 302 Feb 21, 2022
🍸A Slim Web Application Template

Gracili What is Gracili? Gracili is a PHP Application Template to quickly create a new Project. Using this template can save you a lot of time. With t

Björn Pfoster 1 May 12, 2021