Question

I've used Slim for several projects and I always was connecting routes to class methods, by doing $app->route('/', array('MyClass', 'method'));

However, after installing it with composer, this is the error I'm getting:

call_user_func_array() expects parameter 1 to be a valid callback, non-static method Pages::home() should not be called statically

Here's the code:

class Pages {
  function home() {
    print 'home';
  }
}

$app = new \Slim\Slim();
$app->get('/', array('Pages', 'home'));
$app->run();

Did I miss something? Should I edit my class additionally?

Thanks!

Was it helpful?

Solution

Looks like your error reporting is turned up higher than it has been in the past, as I believe the error you're getting is an E_STRICT error. If you changed your function home() method to public static function home() you'd no longer get the error.

That said, a better solution might be to try the new(-ish) controller feature in Slim 2.4.0. Your new route would look like this:

$app->get('/', '\Pages:home');

OTHER TIPS

The code is wrong, you are passing an array as a parameter instead of the callback. Try passing a function.

Documentation for GET Routes: http://docs.slimframework.com/#GET-Routes

class Pages {
  function home() {
    print 'home';
  }
}

$app = new \Slim\Slim();
$app->get('/',function() {
    $page = new Pages();
    $page->home(); 
});
$app->run();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top