Helpers
Introduction
Lumberjack provides a handful of helpful functions that should make your life a little easier.
By default they are available as static methods on the \Rareloop\Lumberjack\Helpers
class, so that the global namespace isn't getting polluted. You can safely use these methods without fear of function names clashing.
You can tell Lumberjack to create global functions for you if you do not mind about adding these functions to the global namespace.
If you are building packages or plugins specifically for Lumberjack, you cannot rely on the global helper functions as the theme may not have made them available.
Adding Global Helpers
In order to use the global helper functions, all you need to do it tell composer to autoload vendor/rareloop/lumberjack-core/src/functions.php
.
Add the following to your top-level composer.json
file:
Then, tell composer to regenerate its list of autoloaded files by running composer dump-autoload
.
Available Helpers
app
The app
helper returns the current reference to the container.
You can resolve objects from the container by passing in the class name or reference into app()
.
config
The config
helper allows you to get values from your config.
By passing in an array to the config
helper you can set config values.
view
The view
helper returns a new Rareloop\Lumberjack\Http\Responses\TimberResponse
.
route
The route
helper generates a URL from a named route.
If the route requires parameters you can be pass an associative array as a second parameter:
redirect
The redirect
helper returns a new Zend\Diactoros\Response\RedirectResponse
, which redirects the user to a given URL.
session
You can use the session
helper to retrieve and store data in the current session. Passing in 1 (string) argument will get the value of that item from the session. Passing in an array of key/value pairs will add each pair to the session.
And using the global function instead:
request
The request
helper returns the current ServerRequest
object, which means you get access to all these available methods.
For example, to get the current url:
back
Returns a RedirectResponse, which will redirect the user to the previous URL.
If you need to pass any data back to the previous page, you can flash items to the session using with()
:
report
Calls the report
method on the Exception Handler, to ensure that an exception is reported.
This is particularly useful if your theme needs to swallow any exceptions so they do not break the site, but you still wish to have the error logged. For example:
"Swallowing" here simply means that the exception is unable to bubble all the way up to the Exception Handler where it normally gets reported.
logger
Available in v4.3.0
and above
The logger
helper can be used to write debug messages to your logs.
You can also pass in an array of additional data alongside your message.
If you need to access the logger class itself, to log different types of errors for example, you can use the logger
function with no arguments. This will get you an instance of the PSR3 compliant logger that is bound to the container. By default Lumberjack uses Monolog\Logger
.
Last updated