dsyq40772 2014-05-15 16:24
浏览 140
已采纳

如何在所需的PHP文件中使用Illuminate数据库实例

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.

  • 写回答

1条回答 默认 最新

  • duanjia7607 2014-05-15 16:39
    关注
    'msg' => print_r(Capsule,true)
    

    try

    'msg' => print_r($capsule,true)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题