Question

<?php
  require 'vendor/autoload.php';

  // Include all controllers
  foreach(glob("controllers/*.php") as $controller)
  {
    include $controller;
  }

  // Instantiate a new Slip application
  $app = new \Slim\Slim(array(
    'debug' => true
  ));

  // HOME CONTROLLER
  $home = new Home;
  $vr = $home->index();

  // Register application routes
  $app->get('/', function () {
      echo $vr;
  });

  // Run application
  $app->run();

This is my controller I want to use controllers and not keep everything in this single file. Anyhow I have a controllers map where I keep all my controllers. I automatically include them all at start however I can't seem to pass $home variable to get() method so I could call $vr indede it or $home->index()

Was it helpful?

Solution

You can pass it to your function like this:

..., function () use($home){
  ...

I think Slim also passes $app as the first argument to your function.

Edit: actually it doesn't according to its docs, so you'll have to pass that too inside the use statement (function arguments are URL parameters):

$app->get('/', function () use($home, $app) {
   $vr = $home->index();
   echo $vr;
   // $app is accesible too...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top