Hello, as the documentation states it is ok to use it if it is a medium app, I basically use micro when I don't need to serve views, just data, and controllers help to keep a better organization of the application.
Here I share my class to work with the routes, watch out that I also have implemented ACL and roles so it also takes care of security at the same time:
use Phalcon\DI\Injectable;
use Phalcon\Mvc\Micro\Collection as MicroCollection;
class RouteCollection extends Injectable {
private $controller;
private $collection;
private $route;
public function __construct($di, $controller) {
$this->setDI($di);
$this->controller = new $controller;
$this->collection = new MicroCollection;
$this->collection->setHandler($this->controller);
$this->route = (object) [
'url' => '',
'method' => '',
'action' => '',
'roles' => [],
];
}
public function add($url) {
$this->route->url = $url;
return $this;
}
public function via($method) {
$this->route->method = $method;
return $this;
}
public function to($action) {
$this->route->action = $action;
return $this;
}
public function by() {
$roles = func_get_args();
$this->collection->{$this->route->method}($this->route->url, $this->route->action);
$this->acl->addResource($this->route->url, strtoupper($this->route->method), $roles);
return $this;
}
public function mount() {
$this->application->mount($this->collection);
}
}
Then to add routes is just like:
$routes = new RouteCollection($di, 'UsersController');
$routes->add('/users')
->via('get')->to('getAll')->by('admin')
->via('post')->to('create')->by('guest', 'admin');
$routes->add('/users/{id:[0-9]+}')
->via('get')->to('get')->by('guest', 'user', 'admin')
->via('put')->to('update')->by('user', 'admin')
->via('delete')->to('delete')->by('user', 'admin');
$routes->add('/users/login')
->via('post')->to('login')->by('guest', 'user', 'admin');
$routes->add('/users/logout')
->via('post')->to('logout')->by('user', 'admin');
$routes->add('/users/find/{alias}')
->via('get')->to('find')->by('guest', 'user', 'admin');
$routes->mount();