Service manager for Slim compatible with Laravel packages

Overview

SlimServices

SlimServices is a service manager for the Slim PHP microframework based on Laravel 4 service providers and DI container, allowing you to use core and many third-party Laravel packages in Slim based projects.

For example, to add Eloquent ORM to your Slim app:

require 'vendor/autoload.php';

use SlimServices\ServiceManager;

$app = new Slim\Slim(array(
	// paths
	'path' => __DIR__,
	// database
    'database.fetch' => PDO::FETCH_CLASS,
    'database.default' => 'main',
    'database.connections' => array(
        'main' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'my_database',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    )
));

$services = new ServiceManager($app);
$services->registerServices(array(
	'Illuminate\Events\EventServiceProvider',
	'Illuminate\Database\DatabaseServiceProvider'
));

// Laravel database component is now available in Slim's DI container

$app->get('/users', function()
{
	$app->render('users.html', array(
		// Load user list using Laravel database fluent query builder
		'users' => $app->db->table('users')->where('active', 1)->get()
	));
})

$app->run();

You can find more information about service providers in the Laravel documentation.

Installation

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

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

Once the package is installed, you need to create a ServiceManager and register the services you'd like to use, configuration for the services is shared with the Slim instance itself:

use SlimServices\Service;

$app = new Slim(...);

$services = new ServiceManager($app);
$services->registerServices(array(
	'Illuminate\Events\EventServiceProvider',
	'Illuminate\Database\DatabaseServiceProvider',
	'Illuminate\Filesystem\FilesystemServiceProvider',
	'Illuminate\Translation\TranslationServiceProvider',
	'Illuminate\Validation\ValidationServiceProvider',
	'Mailer\MailerServiceProvider',
	'Upload\UploadServiceProvider',
	...
));

Configuration examples for some popular components:

Illuminate/Database

=2.3.0", "itsgoingd/slim-services": "dev-master", "illuminate/database": "4.1.*" }">
"require": {
    "slim/slim": ">=2.3.0",
    "itsgoingd/slim-services": "dev-master",
    "illuminate/database": "4.1.*"
}
require 'vendor/autoload.php';

use SlimServices\ServiceManager;

$app = new Slim\Slim(array(
	// paths
	'path' => __DIR__,
	// database
    'database.fetch' => PDO::FETCH_CLASS,
    'database.default' => 'main',
    'database.connections' => array(
        'main' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'my_database',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    )
));

$services = new ServiceManager($app);
$services->registerServices(array(
	'Illuminate\Events\EventServiceProvider',
	'Illuminate\Database\DatabaseServiceProvider'
));

$users = $app->db->table('users')->select('login')->get();

class User extends Illuminate\Database\Eloquent\Model { public $table = 'users'; }

$users = User::all();

Illuminate/Validation

=2.3.0", "itsgoingd/slim-services": "dev-master", "illuminate/validation": "4.1.*", "illuminate/filesystem": "4.1.*", "illuminate/translation": "4.1.*" }">
"require": {
    "slim/slim": ">=2.3.0",
    "itsgoingd/slim-services": "dev-master",
	"illuminate/validation": "4.1.*",
    "illuminate/filesystem": "4.1.*",
    "illuminate/translation": "4.1.*"
}
require 'vendor/autoload.php';

use SlimServices\ServiceManager;

$app = new Slim\Slim(array(
	// paths
	'path' => __DIR__,
	// app
	'app.locale' => 'en'
));

$services = new ServiceManager($app);
$services->registerServices(array(
	'Illuminate\Filesystem\FilesystemServiceProvider',
	'Illuminate\Translation\TranslationServiceProvider',
	'Illuminate\Validation\ValidationServiceProvider'
));

$validator = $app->validator->make(
    array(
        'name' => 'Igor',
        'password' => 'noname',
        'email' => '[email protected]'
    ),
    array(
        'name' => 'required',
        'password' => 'required|min:8',
        'email' => 'required|email|unique:users'
    )
);

if ($validator->fails()) { ... }

Custom service providers

You can create custom service providers simply by extending the Illuminate\Support\ServiceProvider class and registering them with the ServiceManager.

class MailerServiceProvider extends Illuminate\Support\ServiceProvider
{
	public function register()
	{
		$this->app->bindShared('mailer', function($app)
		{
			return new Mailer($app['config']);
		});
	}
}

$services->registerServices(array(
	...,
	'MailerServiceProvider'
));

$app->mailer->send(...);

Licence

Copyright (c) 2014 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.

You might also like...
Collection of agnostic PHP Functions and helpers with zero dependencies to use as foundation in packages and other project

Collection of agnostic PHP Functions and helpers This package provides a lot of very usefull agnostic helpers to use as foundation in packages and oth

Composer plugin for cleaning up unused files from packages.

Composer Cleanup Plugin Remove tests & documentation from the vendor dir. Based on laravel-vendor-cleanup but implemented as a Composer Plugin instead

🖼️ Laravel Nova Field for uploading and cropping images using Slim Image Cropper
🖼️ Laravel Nova Field for uploading and cropping images using Slim Image Cropper

🖼️ Laravel Nova Field for uploading and cropping images using Slim Image Cropper

laravel - Potion is a pure PHP asset manager for Laravel 5 based off of Assetic.

laravel-potion Potion is a pure PHP asset manager for Laravel based off of Assetic. Description Laravel 5 comes with a great asset manager called Elix

Laravel-tagmanager - An easier way to add Google Tag Manager to your Laravel application.

Laravel TagManager An easier way to add Google Tag Manager to your Laravel application. Including recommended GTM events support. Requirements Laravel

A Simple PHP Renderer for Slim 3 (or any other PSR-7 project)

PHP Renderer This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 4. Cross-site scripting

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 Jam is a demo application to provide examples for composer package, PHPSpreadsheet, Shopify API etc. usages.

SLIM JAM Slim Jam is a demo application to provide examples for composer package, PHPSpreadsheet, Shopify API etc. usages. This project aims to take a

Blade is a simple, yet powerful templating engine provided for the Slim Framework

slim-blade Blade is the default template engine of Laravel. The main advantage of Blade is template inheritance whilst using plain PHP. This package a

Comments
  • laravel paginate

    laravel paginate

    $rows = Company::where('is_active', 1)->paginate(10);

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Slim Application Error The application could not run because of the following error:

    Details

    Type: ReflectionException Code: -1 Message: Class request does not exist File: /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php Line: 501 Trace #0 /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php(501): ReflectionClass->__construct('request') #1 /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php(425): Illuminate\Container\Container->build('request', Array) #2 /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php(809): Illuminate\Container\Container->make('request') #3 /Users/tuva/Sites/ticmer/vendor/illuminate/pagination/Illuminate/Pagination/PaginationServiceProvider.php(23): Illuminate\Container\Container->offsetGet('request') #4 /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php(207): Illuminate\Pagination\PaginationServiceProvider->Illuminate\Pagination{closure}(Object(TuvaServices\ServiceManager)) #5 /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php(498): Illuminate\Container\Container->Illuminate\Container{closure}(Object(TuvaServices\ServiceManager), Array) #6 /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php(425): Illuminate\Container\Container->build(Object(Closure), Array) #7 /Users/tuva/Sites/ticmer/vendor/illuminate/container/Illuminate/Container/Container.php(809): Illuminate\Container\Container->make('paginator') #8 /Users/tuva/Sites/ticmer/vendor/illuminate/database/Illuminate/Database/DatabaseManager.php(160): Illuminate\Container\Container->offsetGet('paginator') #9 [internal function]: Illuminate\Database\DatabaseManager->Illuminate\Database{closure}() #10 /Users/tuva/Sites/ticmer/vendor/illuminate/database/Illuminate/Database/Connection.php(837): call_user_func(Object(Closure)) #11 /Users/tuva/Sites/ticmer/vendor/illuminate/database/Illuminate/Database/Eloquent/Builder.php(225): Illuminate\Database\Connection->getPaginator() #12 /Users/tuva/Sites/ticmer/app/routes/site.php(101): Illuminate\Database\Eloquent\Builder->paginate(2) #13 [internal function]: {closure}() #14 /Users/tuva/Sites/ticmer/vendor/slim/slim/Slim/Route.php(468): call_user_func_array(Object(Closure), Array) #15 /Users/tuva/Sites/ticmer/vendor/slim/slim/Slim/Slim.php(1338): Slim\Route->dispatch() #16 /Users/tuva/Sites/ticmer/vendor/slim/slim/Slim/Middleware/Flash.php(85): Slim\Slim->call() #17 /Users/tuva/Sites/ticmer/vendor/slim/slim/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call() #18 /Users/tuva/Sites/ticmer/vendor/slim/slim/Slim/Middleware/SessionCookie.php(110): Slim\Middleware\MethodOverride->call() #19 /Users/tuva/Sites/ticmer/vendor/slim/slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\SessionCookie->call() #20 /Users/tuva/Sites/ticmer/vendor/slim/slim/Slim/Slim.php(1283): Slim\Middleware\PrettyExceptions->call() #21 /Users/tuva/Sites/ticmer/index.php(97): Slim\Slim->run() #22 {main}

    opened by tuvaergun 0
Owner
its
webdev underground
its
Laravel Manager provides some manager functionality for Laravel

Laravel Manager Laravel Manager was created by, and is maintained by Graham Campbell, and provides some manager functionality for Laravel. Feel free t

Graham Campbell 371 Jul 11, 2022
A laravel service provider for the netsuite-php library service

netsuite-laravel A PHP supplemental package to the ryanwinchester/netsuite-php package to add the NetSuite service client to the service container of

NetsuitePHP 6 Nov 9, 2022
The fastest way to make a powerful JSON:API compatible Rest API with Laravel.

The first fully customizable Laravel JSON:API builder. "CRUD" and protect your resources with 0 (zero) extra line of code. Installation You can instal

BinarCode 288 Aug 8, 2022
An HTTP Interop compatible middleware dispatcher in Equip

An HTTP Interop compatible middleware dispatcher in Equip. Attempts to be PSR-1, PSR-2, PSR-4, PSR-7, and PSR-15 compliant.

Equip 24 Aug 10, 2022
Simple PSR-7 compatible response sender

Simple PSR-7 compatible response sender

Lazzard 4 Nov 5, 2022
📦 Adds Laravel Packages Support to Lumen and Vendor Publish Artisan Command.

Laravel Package Support for Lumen: Makes Lumen compatible with Laravel Packages. You can use any Laravel Packages in Lumen by installing Larasupport Package.

Irfaq Syed 127 Dec 17, 2022
A package for Laravel to perform basic git commands on locally integrated packages.

A package for Laravel to perform basic git commands on locally integrated development packages. If working within multiple local development packages or repositories at once this package is meant to ease the burden of navigating to each individual repository to perform basic git commands.

null 3 Jul 26, 2022
Watch your Laravel app for unwanted changes when working with third-party packages.

Project Secure This package installs a Composer plugin that reports unwanted changes to your Laravel project code after installing or updating a third

The Laravel Hacker 3 Nov 3, 2021
Tools for creating Laravel packages

Tools for creating Laravel packages This package contains a PackageServiceProvider that you can use in your packages to easily register config files,

Spatie 526 Dec 29, 2022
Allow your model to record the creation, update and deletion of user fingerprints in laravel packages

This laravel package will allow your models to record the the created, updated and deleted by User FingerPrints

Managemize 4 Mar 11, 2022