Download the PHP package affinity4/slimphp-support without Composer
On this page you can find all versions of the php package affinity4/slimphp-support. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download affinity4/slimphp-support
More information about affinity4/slimphp-support
Files in affinity4/slimphp-support
Package slimphp-support
Short Description Add Laravel style facades, traits and helper functions to any SlimPHP app
License MIT
Informations about the package slimphp-support
SlimPHP Support
Add Laravel style facades, traits and helper functions to any SlimPHP app
Installation
Usage
Setting up Facades in your Application
To use SlimPHP Facades, you first need to create your Slim app as normal, with either Slim\App\AppFactory
or DI\Container\Slim\Bridge
. Then you'll need to call Affinity4\SlimSupport\Support\Facade::setFacadeApplication($app)
:
You will now have access to all Facades, as well as the helper function (e.g. response()
)
App Facade
Facade for Slim\App
:
Container
Response
JSON Response
Pipeline Facade
NOTE: See Pipeline Support Class section for a detailed example.
Helper functions
response()
Standard application/text Response
Standard JSON Response
tap()
Traits
Tappable
Macroable
Macros allow you to add methods to classes dynamically (without having to modify their code).
Let's say you are tired of having to do this:
Instead you just want to call a write method directly from the $response
instance. First, we need to extend the Response class so we can use the Macroable
trait, but still have all of our base Response methods.
Then we need to add MacroableResponse
to our container, so we are always dealing with the same instance (not all instances will have the "macroed" methods).
Then we can get our MacroableResponse
instance from the container however you want, and just call write
!
Conditionable
Allows to conditionally chain functionality.
For example, let's imagine we have a standard PSR-11 Container, which has a the bare minimum PSR-11 compliant methods, set
, get
and has
. The set
method adds a service to the container, get
returns the service and has
checks an service is in the container.
We have a Logger
we want to add to the container, but it requires a FileDriver
to be in the container already, or else we need to also add the FileDriver
class to the container first.
We might then have some bootstrapping logic like so:
However, if we extends our Container
class and add the Conditionable
trait, we can instead use the unless
method to do this check with a fluent interface:
NOTE: To check the opposite, there is also when
.
You're probably thinking this is still quite bit verbose, so to clean this up you could create invokable
ServiceFactory classes for all of your $container->set
logic.__
Dumpable
Adds dump
and dd
methods to any class
Would result in:
NOTE: You can also pass ...$args
to the dd and dump methods as normal if you want to append additional dump data.
ForwardsCalls
Proxy calls to missing methods in current class, to another target class. Useful when you cannot inherit or modify a class but you want to add some functionality to it (other than overloading any of it's methods of course).
Here's an example where we have a base App
class, but it is a final class so we cannot inherit it. So instead, we create an AppProxy
class which allows us to say that "any method that gets called on AppProxy
which doesn't exist in AppProxy
, we use App
instead"
Then we can use getContainer
(or any other public methods/properties) from App
by calling out AppProxy
Pipeline Support class
Pipelines allow for a middleware-like interface to chain processing of tasks.
A pipeline processes each task, passed the returned value to the next process in the chain.
They are useful for multi-step data processing, http middleware, database querying and validation tasks.
Here's an example of how to use it to validation, filter, transform and save an incoming get request.
This way our controller stays clean, and readable, and each responsibility is separated to it's own class to make maintainance easier in the long run. This would also make testing easier, as you could test the individual classes, and also the overall pipeline result, without needing to test the controller itself.
Hub
A Hub
class, is a way to store a similar group of pipelines so they can be retrieved and executed from the same object.