2

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

Sahbaz
  • 1,242
  • 4
  • 17
  • 39
  • If you're using Slim skeleton app, then dependencies.php is probably a good place to put set up code. – Nima Sep 24 '17 at 18:49
  • Can you maybe write some code sample of that so i can see how i should do it ? – Sahbaz Sep 24 '17 at 19:39
  • You already wrote the code! The first block of code, the `use` statement and `R::setup` call Just move it to `src/dependencies.php`. Did you face any problems using your code that you decided to ask about it on SO? Your code seems fine. – Nima Sep 24 '17 at 19:43
  • i updated my question, as you can see i added everything to dependencies.php but in other file for example route.php i can not use red beans – Sahbaz Sep 24 '17 at 19:49
  • 1
    Now you just need to add `use \RedBeanPHP\R as R;` to your routes.php as well. Only once, then you can use `R::load` inside your route callbacks. – Nima Sep 24 '17 at 19:55
  • Thx its working, i didnt know that i need to add use \RedBeanPHP\R as R; both in dependencies.php and routes.php. So can you confirm this is good structure. (i edited my quesiton) – Sahbaz Sep 24 '17 at 20:05
  • The structure is fine now, but it is not a good idea to incorporate the solution in the question. Now your question is not a question anymore. – Nima Sep 24 '17 at 20:11
  • I know you should put answer so i can mark it as solution. Thx again! – Sahbaz Sep 24 '17 at 20:12

2 Answers2

5

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)

Nima
  • 3,309
  • 6
  • 27
  • 44
0

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 :-)