Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/akrabat/rka-slim-controller
Dynamically instantiated controller classes for Slim Framework 2
https://github.com/akrabat/rka-slim-controller
Last synced: 2 months ago
JSON representation
Dynamically instantiated controller classes for Slim Framework 2
- Host: GitHub
- URL: https://github.com/akrabat/rka-slim-controller
- Owner: akrabat
- License: other
- Created: 2014-12-15T09:47:29.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-04-09T20:57:11.000Z (almost 10 years ago)
- Last Synced: 2024-11-01T08:42:18.469Z (2 months ago)
- Language: PHP
- Homepage:
- Size: 265 KB
- Stars: 48
- Watchers: 8
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RKA Slim Controller
An extension to [Slim Framework][1] that allows you use to dynamically
instantiated controllers with action methods wherever you would use a
closure when routing.The controller can optionally be loaded from Slim's DI container,
allowing you to inject dependencies as required.[1]: http://www.slimframework.com/
## Installation
composer require akrabat/rka-slim-controller
## Usage
Use the string format `{controller class name}:{action method name}`
wherever you would usually use a closure:e.g.
$app = new \RKA\Slim();
$app->get('/hello:name', 'App\IndexController:home');You can also register the controller with Slim's DI container:
$app = new \RKA\Slim();
$app->container->singleton('App\IndexController', function ($container) {
// Retrieve any required dependencies from the container and
// inject into the constructor of the controllerreturn new \App\IndexController();
});$app->get('/', 'App\IndexController:index');
## Controller class methods
*RKA Slim Controller* will call the controller's `setApp()`, `setRequest()`
and `setResponse()` methods if they exist and populate appropriately. It will
then call the controller's `init()`` method.Hence, a typical controller may look like:
app = $app;
}public function setRequest($request)
{
$this->request = $request;
}public function setResponse($response)
{
$this->response = $response;
}// Init
public function init()
{
// do things now that app, request and response are set.
}
}## Example project
Look at [slim-di](https://github.com/akrabat/slim-di).