Can't access $kirby object inside of routes in config.php

I’m trying to send an email to the owner of a site when a form is submitted. On the front-end, I’m using Vue, where I have an input that makes an axios request to an endpoint when the input is submitted.

I’m then listening to this endpoint in my /site/config/config.php file, and trying to send the submitted data in an email. The endpoint works fine if I’m just returning testing data, but as soon as I add the $kirby object into the endpoint action to send an email with kirby email, I keep getting 500 errors. Any ideas? Here is the code in my config.php.


return [
    'routes' => function ($kirby) {
        return [
                'pattern' => 'email-signup/(:all)',
                'action' => function ($all) {
                    // $all is the email address data
                    $body = 'The following email address wants to be signed up: ' . $all;

                    try {
                            'from' => '',
                            'to' => '',
                            'subject' => 'Welcome!',
                            'body'=> $body,
                    } catch (Exception $error) {
                      return $error;

                    return 'success';
                'method' => 'POST'

Have you tried using kirby() instead of $kirby?

e.g. kirby()->email($array)

1 Like

Ahh that worked perfectly! Thanks!

I also found that $kirby->whatever() does not work for me, but kirby()->whatever() does. So I’m using kirby().

The documentation uses $kirby a lot though. Is it ok to just use kirby() instead everywhere?
What would I need to do to be able to use $kirby? The reference states that $kirby = kirby() creates the $kirby object. Where would it have to be defined to accessible throughout my project?

The $kirby variable is accessible everywhere in templates/snippets/controllers. In plugins/routes/hooks, you can either use the kirby() helper or define $kirby = kirby().

1 Like