Skip to content

Instantly share code, notes, and snippets.

@pentagonal
Created May 20, 2017 11:41
Show Gist options
  • Save pentagonal/0423ecfb343cdca76fe9f27584fd8219 to your computer and use it in GitHub Desktop.
Save pentagonal/0423ecfb343cdca76fe9f27584fd8219 to your computer and use it in GitHub Desktop.
<?php
/**
* Example Class
*/
class MyRoutes
{
/**
* @var \Slim\App $slim
*/
protected $slim;
/**
* @param \Slim\App $slim
*/
public function __construct(\Slim\App $slim)
{
$this->slim = $slim;
}
protected function build()
{
// do the routes
$this->slim->any('/', [$this, 'home']);
$this->slim->post('/login[/]', [$this, 'login']);
$this->slim->get('/article/{article_id: [a-9]([0-9]+)?}[/]', [$this, 'article']);
}
public function home($request, $response)
{
return $response;
}
public function login($request, $response)
{
return $response;
}
public function article($request, $response, $param = [])
{
$article_id = isset($param['article_id'])
? $param['article_id']
: null;
return $response;
}
// only tricky
public function __invoke()
{
$this->build();
}
}
<?php
/* ------------------------------------------------------ *\
| APPLICATION COMPONENT ROUTES |
\* ------------------------------------------------------ */
namespace {
use PentagonalProject\ProjectSeventh\Application;
use PentagonalProject\ProjectSeventh\Arguments;
use Slim\App;
if (!isset($this) || ! $this instanceof Arguments) {
return;
}
/** @var Application $c */
$c =& $this[CONTAINER_APPLICATION];
if (!$c instanceof Application) {
return;
}
/**
* @var App $slim
*/
$slim =& $c->getSlim();
// start routes collections GROUP start with '(.*)?'
$slim->group('', new MyRoutes($slim));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment