Bow Package
Introduction​
This system lets you extend your application by grafting external packages into a Bow application.
Creating a plugin​
If you have a Bow application, you simply create it through php bow by running the add:configuration command, then provide the name of your configuration:
php bow add:configuration PluginConfiguration
The PluginConfiguration.php file will be created in the app/Configuration folder, which contains two methods, create and run.
createinitializes the packagerunruns the package initialization.
When initializing packages, the create method runs first for all the other packages defined above it in your configuration, and then the run method runs in the same order.
Do not try to reach a package here if you are expecting it to be configured first.
In the configuration file, Bow injects the DIC, which is accessible through the $this->container property. More information about Bow's DIC.
Creation example​
We are going to build a plugin that validates an email, so we create the configuration with the name EmailCheckerConfiguration:
Note that this is an example to show you how to get started here; in a real project, this example could have been handled differently.
php bow add:configuration EmailCheckerConfiguration
In the app folder we will add a Packages folder, and inside that folder we will also add the EmailCheckController.php file:
namespace App\Packages;
use Bow\Http\Request;
use Bow\Support\Str;
class EmailCheckController
{
/**
* Check the email
*
* @param Request $request
* @return mixed
*/
public function __invoke(Request $request)
{
$email = $request->get('email');
if (Str::isMail($email)) {
$response = [
'message' => 'Ok',
'error' => false
];
} else {
$response = [
'message' => 'C\'est un mauvais email !',
'error' => true
];
}
return $response;
}
}
We will now create our configuration:
namespace App\Configurations;
use Bow\Configuration\Loader;
use Bow\Configuration\Configuration;
class EmailCheckController extends Configuration
{
/**
* Permet de lancement de configuration
*
* @return void
*/
public function run()
{
$this->container->make('app')->post(
'/email/checker', \App\Packages\EmailCheckController::class
);
}
}
Let's test our BowPHP application with the php bow run:server command to start the development server.
Testing our package​
For this example we will test our code with curl:
curl -X POST -d "email=exemple@exemple.com" http://localhost:5000/email/checker
# {"message": "Ok", "error": false}
Is something missing?
If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.