Download the PHP package matiasnamendola/slimpower-slim without Composer
On this page you can find all versions of the php package matiasnamendola/slimpower-slim. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download matiasnamendola/slimpower-slim
More information about matiasnamendola/slimpower-slim
Files in matiasnamendola/slimpower-slim
Package slimpower-slim
Short Description Dynamically instantiated controller classes for Slim Framework
License MIT
Homepage https://github.com/matiasnamendola/slimpower-slim-controller
Informations about the package slimpower-slim
SlimPower - Slim
An extension to Slim Framework that allows you use to dynamically instantiated controllers with action methods wherever you would use a closure or callback when routing.
The controller can optionally be loaded from Slim's DI container, allowing you to inject dependencies as required.
Additionally, this extension implements Json Middleware & View with great ease.
Installation
Look at Installation File
Usage - Dynamic controller instantiation
Use the string format {controller class name}:{action method name}
wherever you would usually use a closure:
e.g.
You can also register the controller with Slim's DI container:
Example controller
SlimPower - 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:
Usage - Json Middleware
To include the middleware and view you just have to load them using the default Slim way. Read more about Slim Here (https://github.com/codeguy/Slim#getting-started)
Example method
All your requests will be returning a JSON output.
the usage will be $app->render( (int)$HTTP_CODE, (array)$DATA);
Example code
Example output
Errors
To display an error just set the error => true
in your data array.
All requests will have an error
param that defaults to false.
You can optionally throw exceptions, the middleware will catch all exceptions and display error messages.
Embedding response data and metadata in separate containers
It is possible to separate response metadata and business information in separate containers.
To make it possible just init JsonView with containers names
Response
Routing specific requests to the API
If your site is using regular HTML responses and you just want to expose an API point on a specific route, you can use Slim router middlewares to define this.
Middleware
The middleware will set some static routes for default requests. if you dont want to use it, you can copy its content code into your bootstrap file.
IMPORTANT: remember to use $app->config('debug', false);
or errors will still be printed in HTML
Credits
License
The MIT License (MIT). Please see License File for more information.
Example project
Look at slimpower-slim-example.