Example #1
0
 public function __construct(Config $cfg, AbstractModule $mod, array $injections = null)
 {
     parent::__construct($cfg, $mod, $injections);
     // Add the general options.
     $cfg->addKnownOptions(['controllerNamespace' => 'Controllers', 'controllerSuffix' => '', 'alternativeMethodSuffix' => 'Action', 'modelSuffix' => '', 'entitySuffix' => '', 'notFoundHandler' => 'Classes\\NotFound->index', 'defaultController' => 'Controllers\\Index', 'defaultMethod' => 'index', 'moduleClassName' => __CLASS__, 'contentType' => 'application/json']);
     // Options for mapping resources to controllers.
     $cfg->addKnownOptions('resourceMappings', ['index' => 'Index']);
     $cfg->refreshCache();
     $this->router->addFilters(['resource' => function ($val) {
         $resources = $this->config->getSet('resourceMappings');
         return is_array($resources) && array_key_exists($val, $resources) ? true : false;
     }]);
     // Filters for the Restful functionality
     $this->router->appendRoutes(['/:resource/(*params)?' => function ($resourceName, $params = '/') {
         if ($class = $this->getResourceController($resourceName)) {
             $c = new $class($this->config, $this, $params);
             return $c->response;
         }
         return false;
     }, '/*params' => function ($params) {
         return false;
         // Not found.
     }, '/' => function () {
         //Check if the global handler method exists
         return $this->callGlobalIfDefined();
     }]);
 }
Example #2
0
 public function __construct(Config $cfg, AbstractModule $parent = null, array $injections = null)
 {
     parent::__construct($cfg, $parent, $injections);
     // Add the options related to this module.
     $cfg->addKnownOptions(['controllerNamespace' => 'Controllers', 'controllerSuffix' => '', 'alternativeMethodSuffix' => 'Action', 'viewsDir' => 'views', 'modelSuffix' => '', 'entitySuffix' => '', 'notFoundHandler' => 'Classes\\NotFound->index', 'defaultController' => 'Index', 'defaultMethod' => 'index', 'moduleClassName' => __CLASS__]);
     $cfg->refreshCache();
     //Determine if any of this routes actually exists as an object
     $this->router->addFilters(array('controller' => function ($name, $path) {
         $ns = $this->config->namespace . '\\' . ucwords(preg_replace(['/^\\//', '/\\//'], ['', '\\'], $path)) . $this->config->controllerNamespace . '\\';
         $class = $ns . ucwords($name) . $this->config->controllerSuffix;
         if (class_exists($class, true)) {
             return true;
         }
         return false;
     }));
     // Filters for the MVC Controller functionality
     $this->router->appendRoutes(['/:controller/:method/{*params}?' => array($this, 'captureControllerMethod'), '/:controller/{*param}?' => array($this, 'captureController'), '/{*param}' => array($this, 'notFoundHandler'), '/' => array($this, 'captureDefault')]);
 }