4.D Global Package Methods
Global Package Methods are virtual methods accessible using cradle('global')
,
cradle()->package('global')
or $this->package('global')
depending on which
scope you are in. The majority of the Global Package Methods are defined in
the /bootstrap
folder to allow you to manipulate it on your custom projects.
4.D.1. path()
Given a key, returns the absolute path relative to your project. The possible keys are as follows.
root
-[root path]
app
-[root path]/app
bootstrap
-[root path]/bootstrap
config
-[root path]/config
module
-[root path]/module
compiled
-[root path]/compiled
public
-[root path]/public
upload
-[root path]/upload
template
-[root path]/template
vendor
-[root path]/vendor
Usage 1
cradle('global')->path('config');
You can also set more paths like the following example.
Usage 2
cradle('global')->path('foo', __DIR__ . '/bar');
4.D.2. config()
Retrieves a configuration from your /config/
folder.
Usage 1
//returns the array found in config/settings.php
cradle('global')->config('settings');
//returns the value of environment found in config/settings.php which is 'dev'
cradle('global')->config('settings', 'environment');
You can also write to a configuration file like the following example.
Usage 2
//sets foo to bar in config/settings.php
cradle('global')->config('settings', 'foo', 'bar');
//creates a file config/foobar.php and sets foo to bar
cradle('global')->config('settings', 'foobar', [
'foo' => 'bar'
]);
Make sure you chmod 777 your config folder
4.D.3. service()
Returns a service object.
Usage 1
cradle('global')->service('sql-main'); //--> PDO
cradle('global')->service('elastic-main'); //--> Elasticsearch\ClientBuilder
cradle('global')->service('redis-main'); //--> Predis\Client
cradle('global')->service('rabbitmq-main'); //--> PhpAmqpLib\Connection\AMQPLazyConnection
If the key you provide is not a supported service, the array configuration will
return.
4.D.4. redirect()
A wrapper to redirect to another URL
Usage
cradle('global')->redirect('/some/path');
4.D.5. requireLogin()
Checks to see if the user is logged in and redirects to the login page if not.
Usage
cradle('global')->requireLogin();
4.D.6. flash()
Sets a flash message that will be consumed on the next URL request.
Usage
cradle('global')->flash('Something good happened', 'success');
cradle('global')->flash('Something happened', 'warning');
cradle('global')->flash('Something bad happened', 'error');
cradle('global')->flash('Something', 'info');
4.D.7. translate()
Translate the given string based on the current language
Usage
$name = cradle('global')->translate('Name'); //--> nom
4.D.8. template()
Renders a template using Handlebars.
Usage
$handlebars = cradle('global')->template('/path/to/template', [
'foo' => 'bar',
'zoo' => 'Went to the zoo.'
]);
4.D.9. handlebars()
Returns the global Handlebars instance
Usage
$handlebars = cradle('global')->handlebars();