0

I am trying to setup a restful API using Slim PHP. This is my first time using a microframework and I am trying to structure an application with it so it remains maintainable.

I am trying to structure my application like so:

root
- index.php
- composer.json
- app
- - routes
- - - session.php
- - lib
- - - functions.php
- - vendor
- - - Composer Vendor dirs and files

I am using illuminate to access my MySQL database and while I had all my code in just the index.php file everything worked. When I tried to split up my authentication functions into the session.php I was no longer able to access the illuminate instance. I followed the standard setup from the github page

use \Slim\Slim;
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
'driver'    => 'mysql',
'host'      => 'localhost',
'database'  => 'database',
'username'  => 'root',
'password'  => 'password',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
]);

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

$app = new Slim();

//get login routes
require 'app/routes/session.php';

$app->get("/", function() use ($app){

$app->render(200,array(
        'msg' => 'welcome',
    ));
});

Here is my session.php:

$app->post("/login", function() use ($app){
    $postVarsAll = $app->request->post();
    if( isset($postVarsAll['password']) && isset($postVarsAll['username']))
    {
        //Get associated user
        $app->render(200,array(
            'msg' => print_r(Capsule,true)
        ));
        $user = Capsule::table('user')->where('username','=',$postVarsAll['username'])-  >first();
    }else{
    $app->render(200, array(
        "msg" => "no params",
        "params" => $postVarsAll
    ));
    }

});

This throws a 500 error and tells me:

"msg": "NOTICE: Use of undefined constant Capsule - assumed 'Capsule'",

I am looking to just use the Capsule instance inside my session.php. Does anyone know how to do this?

If I take out the Capsule calls the route works.

Thanks for your help!

Edit

$app->post("/login", function() use ($app,$capsule){
    $postVarsAll = $app->request->post();
    if( isset($postVarsAll['password']) && isset($postVarsAll['username']))
    {
        //Get associated user
        $app->render(200,array(
            'msg' => print_r($capsule,true)
        ));
     return;
     ...

with this I get the full capsule object.

Edit 2

Thank you volkinc for finding the answer. I can use $capsule->table("user")->... for my query needs.

4

1 回答 1

2
'msg' => print_r(Capsule,true)

try

'msg' => print_r($capsule,true)
于 2014-05-15T16:39:10.410 回答