This post will show you how to re-use your already-exists Zend Framework 2 service manager and modules you have created,
into a SlimPHP application which will handle restfull routing.
You can also use Zend Framework 2 request and response object to create your restful web app
Zend\Mvc\Router\Http\Method and|or extends
but this time, in my case, I want to play with SlimPHP and call my super awesome Zend Framework 2 oauth2-server service
for SlimPHP’s ‘slim.before.dispatch’ validation and also call another service from SlimPHP’s controllers,
so this SlimPHP will just handle, validate, receives request and giving various json responses for my ReST API,
but still anything behind will handled by Zend Framework 2 services.
Okay enough talk, this super simple code will load all my Zend Framework 2 modules and will be ready-to-call in my SlimPHP app (without calling entire Zend Framework 2 McvEvent) :
<?php use Slim\Slim as SlimApplication; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager\ServiceManager; $zf2Config = include_once '/path/to/your/zf2/application/config/config.php'; $sm = new ServiceManager(); $smConfig = new ServiceManagerConfig(); $smConfig->configureServiceManager($sm); $sm->setService('applicationconfig', $zf2Config); $sm->get('ModuleManager')->loadModules(); $slim = new SlimApplication(); // you can use this in your slim app, // via Slim::getInstance()->serviceManager // or whatever you name it $slim->serviceManager = $sm; unset($zf2Config); unset($smConfig); unset($sm); // $slim->hook(); // $slim->notFound(); // $slim->error(); // $slim->get(); // $slim->post(); // $slim->put(); // $slim->patch(); // $slim->delete();
Zend Framework 2 ServiceManager, EventManager and ModuleManager will do the magic, trust me, they are powerfull ;) and now you can use SlimPHP as ReSTful request handler, and call your awesome Zend Framework 2 modules, and services to handle CRUD processing and anything-related-to-it.