2

I am trying slim for the first time and I liked the concept of micro frameworks. I love the fact that I can manage my own database layer rather than relying on ORMs, so rest apart I am stuck adding my own classes in slim app. I went through Slim docs and it was not that much clear what should I use to add my own classes in the app. I am using Middleware for now and this is how I am trying to implement.

Directory structure

enter image description here

Index file:

<?php

require 'Slim\Slim.php' ;
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->add(new \models\MyDb());
$app->config(array(
    'templates.path' => 'templates',
));

$app->get('/', function() use ($app){
    $data = $db->testApp();
    $app->render('main.php', $data);
});

$app->run();

?>

my database wrapper that I would like to implement:

models/db.php

class MyDb extends ..\Slim\Middleware{
public function testApp(){
    /* Returns something */
 }
}

Error that is returned
Fatal error: Class 'models\MyDb' not found in C:\xampp\htdocs\slimtest\index.php on line 6

I know I am making really simple mistake or I am over thinking the implementation but kindly explain in detail as I am fairly new to Slim and Micro framework in general.

I have went through these questions on stack-overflow:
hooks versus middleware in slim 2.0
Routing with AngularJS and Slim PHP

but they were not of that much help plus I don't like going with procedural way and to write all logic in one file.

Note: I have tried replacing slashes and directory structure but no luck.

Community
  • 1
  • 1
Shashi
  • 474
  • 6
  • 21

2 Answers2

3

I'm not Slim expert but there are multiple problems with your code.

Your files structure should be:

models
 --> MyDb.php
Slim
 --> original slim files
templates
 --> main.php
index.php (main file runnning the app)

if you want to use PSR autoloader from Slim framework.

Your MyDb.php file should look like this:

<?php

namespace models;

class MyDb extends \Slim\Middleware{
    public function testApp(){
        return "this is my sample data";
    }

    public function call()
    {
        $this->next->call();
    }
}

call() method must be implemented because it extends \Slim\Middleware.

Your index.php should look like this:

<?php

require 'Slim\Slim.php' ;
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->config(array(
        'templates.path' => 'templates',
    ));

$app->get('/', function() use ($app){
        $db = new \models\MyDb();
        $data = $db->testApp();
        $app->render('main.php', array('data' => $data));
    });

$app->run();

?>

In your earlier code $db was undefined variable.

When your main.php file look like this:

hello world   <?= $data ?>

output will be:

hello world this is my sample data

as expected

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • I tried to implement this but again I got an error, It is not getting the Class. **Fatal error: Class 'models\MyDb' not found in C:\xampp\htdocs\slimtest\index.php on line 12**. Didn't we have to use the namespace somewhere ? – Shashi Sep 01 '14 at 15:30
  • @Shashi Have you used inserted exact the same code as I showed? It works without a problem. You should look at file names (case sensitive) and put exact the same code as I did in the answer. – Marcin Nabiałek Sep 01 '14 at 16:44
  • yes, I have used exact same code and I have also doubled checked the file names but no luck. I am getting the same error: **Fatal error: Class 'models\MyDb' not found in C:\xampp\htdocs\slimtest\index.php on line 12** – Shashi Sep 01 '14 at 17:16
  • Upon some research I can manage to remove that error but now its throwing another error. **Fatal error: Class 'Slim\Middleware' not found in C:\xampp\htdocs\slimtest\models\db.php on line 5** – Shashi Sep 01 '14 at 17:22
  • @Shashi It works without a problem (I've tested it again). In Slim directory there is Middleware.php file so autoloader loads it without a problem. – Marcin Nabiałek Sep 01 '14 at 17:26
  • My Slim folder is outside models folder where the actual db.php resides, so it correct to simply extend the method without moving up a directory **../Slim/Middleware** in place of **\Slim\Middleware**? – Shashi Sep 01 '14 at 17:43
  • I have updated my question with directory structure, please go through that if there is a problem because of directory structure. – Shashi Sep 01 '14 at 17:47
  • @Shashi I don't see any update. Your structure looks exactly the same as at the beginning. Moreover you haven't change `db.php` to `MyDb.php`. I'm afraid I cannnot help you more if you don't read and do what I put in the answer – Marcin Nabiałek Sep 01 '14 at 17:54
  • ahhh! My apologizes, somehow I could't notice the file name, after changing the name it worked fine. – Shashi Sep 01 '14 at 18:00
0

You have chosen a hard path to live. May I suggest a framework where it is done just by $database->add ('\name\of\your\class') ?

Take a look at short showcase of that framework (called db.php) https://github.com/hazardland/db.php/blob/master/samples/001.showcase.php

http://dbphp.net is code first style object relational mapper it even creates databases and synchs everything on the fly if you wish.

I think Slim (and not only Slim but other ORMs also) do things not very intuitively.

BIOHAZARD
  • 1,937
  • 20
  • 23
  • 1
    I understand, but I have already something similar like this although, definitely not that scale but I am looking for clean routes and few other things and I believe Slim was perfect for that. I am not looking for ORMs and If i am not wrong your answer was more of a suggestion rather than a straight answer so **this should be in comment section.** – Shashi Sep 08 '14 at 15:26