Skip to content

Extending Recipes

Available since: brezel-spa@3.9.0, brezel/api@0.79.0

Recipe functions are a powerful way to extend the functionality of your Brezel instance. This helps you to create custom logic that can be reused in your recipes.

Extending recipes in Brezel SPA

Different providers

There are different providers for different parts of the frontend:

  • General recipe provider: used by all parts of the frontend.
  • Layout recipe provider: used by the layout part of the frontend, and extends the general recipe provider.
  • Menu recipe provider: used by the menu part of the frontend, and extends the general recipe provider.

Extending providers

General recipe provider

src/main.ts
import { extendRecipeProvider } from '@kibro/brezel-spa'
extendRecipeProvider((provider) => {
provider.addFunction('foo', () => {
return 'bar'
})
provider.addSymbol('pi', Math.PI)
})

Component recipe provider

src/main.ts
import { extendLayoutRecipeProvider, extendMenuRecipeProvider } from '@kibro/brezel-spa'
extendLayoutRecipeProvider((provider, component) => {
provider.addFunction('getCurrentPath', () => {
return component.$route.path
})
})
extendMenuRecipeProvider((provider, component) => {
provider.addFunction('getCurrentPath', () => {
return component.$route.path
})
})

Extending Recipes in Brezel API

Create a new provider that extends the NativeRecipesDriver and add your functions there.

app/Providers/RecipeProvider.php
<?php
declare(strict_types=1);
namespace MyProject\Providers;
use App\Recipes\Driver\Native\Interpreter\Main\MainInterpreter;
use App\Recipes\Driver\Native\NativeRecipesDriver;
use Illuminate\Support\ServiceProvider;
class RecipeServiceProvider extends ServiceProvider
{
public function boot(NativeRecipesDriver $recipes)
{
$recipes->setInterpreterFactory(function (MainInterpreter $interpreter) {
$interpreter->setFunctionTable(array_merge($interpreter->getFunctionTable(), [
'foo' => fn() => 'bar',
]));
});
}
}

Don’t forget to register your provider in the bootstrap/app.php file.

config/app.php
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use App\Brezel;
use MyProject\Providers\RecipeServiceProvider;
$brezel = new Brezel();
$brezel->setBasePath(realpath(__DIR__ . '/..'));
$brezel->addServiceProvider(RecipeServiceProvider::class);
return $brezel;