在Classes中使用PHP Slim Framework

2 php frameworks slim

我似乎无法使用Slim框架来访问PHP类范围内的函数:

<?php

class Controller {
    private $app;

    public function __construct() {
        $this->app = new Slim();

        $this->app->get('/', $this->home);

        $this->app->run();
    }

    public function home() {
        echo 'hi';
    }
}
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误:

致命错误:未捕获异常'ErrorException',消息'Undefined属性:Controller :: $ home'在/Users/Oliver/Dropbox/Sites/grapevine/application/controller.php:9堆栈跟踪:#0/Users/Oliver/Dropbox /Sites/grapevine/application/controller.php(9):Slim :: handleErrors(8,'Undefined prope ...','/ Users/Oliver/D ...',9,Array)#1/Users/Oliver/Dropbox/Sites/grapevine/public/index.php(14):控制器 - > __ construct()#2 {main}在第9行/Users/Oliver/Dropbox/Sites/grapevine/application/controller.php中引发

我试过这样做:

$this->app->get('/', $this->home());
Run Code Online (Sandbox Code Playgroud)

但是然后忽略路由,并且在每个页面上显示"hi",而不仅仅是'/'.

Thi*_*ter 12

使用成员函数的回调语法:

$this->app->get('/', array($this, 'home'));
Run Code Online (Sandbox Code Playgroud)