2016-05-12 53 views
0

我想要定義一個路由來使用我的課程中的一個方法用戶。苗條3路由不起作用

在的index.php我有這樣的:

$app->post('/user', '\Users:getUsers'); 

在dependencies.php我:

$container['\Users'] = function ($container) { 

return new Users($container->get('ci')); 

}; 

而且在我的課,我有:

class Users { 

protected $ci; 
//Constructor 
public function __construct(ContainerInterface $ci) { 
    $this->ci = $ci; 
} 
public function getUsers(){ 

    $ci = $this->ci; 
    $settings = require __DIR__ . '/../../src/settings.php'; 
    $user = $app = new \Slim\App($settings); 

      $userList =$this->db->table('tbl_test')->get(); 
      $output = array("data" => $userList); 
      return json_encode($output); 

    } 
} 

和錯誤我得到的是類'Users'找不到

任何想法爲什麼?

+0

你在哪裏定義'Users'?你如何使它可用於'index.php'和'dependencies.php'? – alexw

+0

我用'require'嘗試過,但是它給了我一個錯誤,因爲我聲明瞭兩次。所以我試着用'namespace',但它仍然不起作用。像@geggleto說的 –

+0

,絕對考慮使用Composer自動加載。它會讓你的生活變得更輕鬆。要獲得獎勵積分,請按照[PSR-4規格](http://www.php-fig.org/psr/psr-4/)獲取您的課程和名稱空間。 [這](http://phpenthusiast.com/blog/how-to-autoload-with-composer)實際上是一個很好的教程。 – alexw

回答

1

您的用戶類未在您的項目中的任何位置定義。 它可能來自你使用的dependencies.php

它看起來並不像你所使用的自動加載......所以一定要確保你包括說明某處包括在執行類。

您應該爲您的課程使用PSR-4名稱空間。

+0

我正在導入它,但它給了我一個錯誤,因爲我正在重新宣佈它。所以我嘗試命名空間,這也沒有工作。 –