如何正确地将Red Bean PHP添加到我的项目中

I'm not very experienced with php projects structure, I found this awesome and simple tutorial: https://arjunphp.com/creating-restful-api-slim-framework/ how to create simple slim rest app.

This is actually PHP SLIM's official project structure, my question is what is best and proper way to add and use RedBean php ORM, I dont want on every route to include something like this

use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=mydatabase', 'myusername', 'mypassword)

and then

$book = R::load( 'book', $id );

And then use ReadBean for my db stuff. Im wondering how to include RedBeans into project and then just use it where i need it. This is my project structure https://github.com/iarjunphp/creating-restful-api-slim-framework3.

Note: i added red beans via composer like its described here https://github.com/gabordemooij/redbean

You can put the code for setting up your libraries in any file that is going to be included on each request, so assuming you're using slim/slim-skeleton, src/dependencies.php is probably the place you want to add these two lines:

use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=njux_db', 'root', '');

Then you can use ReadBeans in your route callbacks but you also need to add the use \RedBeanPHP\R as R; statement to your src/routes.php as well (or any file that is going to use this class)

If you use a MVC framework (which I recommend) like it's pretty easy.

You only have to copy your rb.php to the application/third_party folder.

Then create a file called application/libraries/rb.php containing a code like this one.

<?php
class Rb { 
  function __construct()    {
    include(APPPATH.'/config/database.php');
    include(APPPATH.'/third_party/rb.php');
    $host = $db[$active_group]['hostname'];
    $user = $db[$active_group]['username'];
    $pass = $db[$active_group]['password'];
    $db   = $db[$active_group]['database'];
    R::setup("mysql:host=$host;dbname=$db", $user, $pass); 
   }
}
?>

...and vôila. RedBean will read your database configuration from CodeIgniter's standard file application/config/database.php and you will be able to use any R:: command from anywhere in your code. No includes, not additional code required :-)