Last active
April 26, 2020 12:37
-
-
Save ziadoz/4332577 to your computer and use it in GitHub Desktop.
Slim Framework Controller Strategies (Simple/Silex)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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