Shashi
Shashi

Reputation: 474

Implementing custom class in Slim

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.

Upvotes: 2

Views: 8437

Answers (2)

BIOHAZARD
BIOHAZARD

Reputation: 2043

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.

Upvotes: 0

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

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

Upvotes: 3

Related Questions