Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Last active April 26, 2020 12:37
Show Gist options
  • Save ziadoz/4332577 to your computer and use it in GitHub Desktop.
Save ziadoz/4332577 to your computer and use it in GitHub Desktop.
Slim Framework Controller Strategies (Simple/Silex)
<?php
// Silex Style Controllers
class App extends \Slim\Slim
{
public function mount($controller)
{
if (! is_object($controller)) {
throw new \InvalidArgumentException('Controller must be an object.');
}
if (! method_exists($controller, 'connect')) {
throw new \BadMethodCallException('Controller must have a connect method.');
}
return $controller->connect($this);
}
}
class Controller
{
protected $app;
public function __construct()
{
$this->app = $app;
}
}
class HomeController extends Controller
{
public function connect()
{
$this->app->get('/hello/:name', array($this, 'indexAction'))->name('hello');
}
public function indexAction($name)
{
return $this->app->render('index.php', array('name' => $name));
}
}
$app = new App;
$app->mount(new HomeController);
$app->run();
<?php
// Simple Style Controllers
class HomeController
{
public function indexAction($name)
{
$app = Slim::getInstance();
return $app->render('index.php', array('name' => $name));
}
}
$app = new \Slim\Slim;
$app->get('/hello/:name', array('HomeController', 'indexAction'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment