Skip to content

Instantly share code, notes, and snippets.

@alexeygon
Forked from ziadoz/app-silex.php
Created October 26, 2015 10:57
Show Gist options
  • Save alexeygon/32a7401f086d1c7325b9 to your computer and use it in GitHub Desktop.
Save alexeygon/32a7401f086d1c7325b9 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