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'找不到
任何想法爲什麼?
你在哪裏定義'Users'?你如何使它可用於'index.php'和'dependencies.php'? – alexw
我用'require'嘗試過,但是它給了我一個錯誤,因爲我聲明瞭兩次。所以我試着用'namespace',但它仍然不起作用。像@geggleto說的 –
,絕對考慮使用Composer自動加載。它會讓你的生活變得更輕鬆。要獲得獎勵積分,請按照[PSR-4規格](http://www.php-fig.org/psr/psr-4/)獲取您的課程和名稱空間。 [這](http://phpenthusiast.com/blog/how-to-autoload-with-composer)實際上是一個很好的教程。 – alexw