How to use Kirby 3's Toolkit classes for other projects

Hi there. Just wondering: does the Kirby Toolkit exists in V3 ?
For a new project I’d just need a simple way to read and post to a database and the toolkit seemed a good option (plus I know already how to install/use it).

Thanks in advance

Yes! I hope this helps: https://github.com/getkirby/kirby/tree/master/src/Toolkit

Since you mention databases, you might also have a look at https://getkirby.com/docs/guide/database

The database class is no longer part of the toolkit but a separate packet.

Cool - How would I do to include only the Database functionalities in my project and initialize/use it?
Is that something you’re ok with? I think the toolkit V2 was free to use in any project

The “Toolkit parts” of v3 are still licensed under the MIT license, so it’s fine to use them for other projects.

Depending on how your project is set up, there are three options to install Kirby:

  • Composer
    You should disable Kirby’s custom installer path for use in other projects, see this example. You can then require_once('vendor/autoload.php') and will have access to the Database classes.
  • Git submodule/manual installation
    Install Kirby as described in the docs to any path you like. Then require_once('kirby/bootstrap.php') to load the classes.

In both cases, the classes are autoloaded, so the classes you don’t use are not loaded.

Great !
I’m using the require_once('kirby/bootstrap.php') method and kirby is loaded correctly because on print_r(kirby()) is shows the kirby object.
Now I’m a bit lost on how to set up a connection to the db because the described way in the guide is through Kirby’s config file, and I don’t know where to configure Kirby’s options. I tried:

  • placing a config file mimicking usual kirby’s folder structure
  • using Config::set()

and in both cases settings seem to be ignored.

This is my project’s folder structure:

neuropa
├─ index.php
├─ assets
├─ back
|  └─ index.php # here I need a db connection
└─ vendor
   ├─ …
   ├─ …
   └─ kirby
      ├─ kirby
      └─ site
         └─ config
            └─ config.php

Ok I’m getting nearer.
When I inspect the options with Config::get() they show up fine and I’m now able to connect to the database.
But with kirby()->options() an empty array was displayed (not sure why).

Other apparent anomalies:

  • calling kirby()->options() after Config::set() produces an error
  • the white error message is displayed instead of the detailed error even if debug is turned on with Config::set('debug', true)

You can establish a database connection without using the config

$params = [
  'host'     => 'localhost',
  'database' => 'database_name',
  'user'     => 'root',
  'password' => 'root',
];

db::connect($params);

I’m having a hard time debugging code using the Db class. For example

$id = Db::insert('sessions_memo', [
  "id"      => "", // tried with and without this
  "added"   => "", // tried with and without this
  "session" => $sessionName,
  "data"    => "{}",
]);
var_dump($id);

just returns false, not sure why. Tried to add try/catch without luck.
Is there a way to see the generated sql code? Other ways to debug?

var_dump(db::trace());

should give you a complete trace of what’s happening.

var_dump(db::lastQuery())

shows the last query

var_dump(db::lastError())

shows the last error

Awesome thanks :pray:!
Are there also ways to do more complex queries such as join or group by ?

Yes, check out the source code.

You could for example use the query() method to execute a raw sql query, but there are many more options.