/** * Method to show the content. * * @return mixed * @throws \Exception If not supported request method or bad controller */ public function render() { if (Sapi::isCli() && Config::get('environment') == 'production') { $suffix = 'CliAction'; $action = $this->request->fromCli()->get('action', 'index'); } else { $suffix = 'Action'; if ($this->request->getMethod() != 'GET' && $this->request->getMethod() != 'POST') { $redirectUrl = new Value($this->env->REDIRECT_URL); $redirectUrl = $redirectUrl->deleteLeading('/')->deleteTrailing('/')->explode('/'); $action = isset($redirectUrl[1]) ? $redirectUrl[1] : 'index'; } else { $bag = sprintf('from%s', ucfirst(strtolower($this->request->getMethod()))); $action = $this->request->{$bag}()->get('action', 'index'); } if (Config::get('app.routeable') === true && $this->router instanceof \Pimf\Router) { $target = $this->router->find(); if ($target instanceof \Pimf\Route\Target) { $action = $target->getAction(); Request::$getData = new Param(array_merge($target->getParams(), Request::$getData->getAll())); } } } $action = strtolower($action) . $suffix; if (method_exists($this, 'init')) { call_user_func(array($this, 'init')); } if (!method_exists($this, $action)) { throw new Bomb("no action '{$action}' defined at controller " . get_class($this)); } return call_user_func(array($this, $action)); }
/** * @param Request $request * @param string $repositoryPath * @param string $prefix * @param \Pimf\Router $router * * @throws \Pimf\Resolver\Exception If no controller found at the repository path */ public function __construct(\Pimf\Request $request, $repositoryPath = '/Controller', $prefix = 'Pimf\\', $router) { $controllerName = $request->fromGet()->get('controller'); $this->router = $router; if (Config::get('app.routeable') === true) { $target = $this->router->find(); if ($target instanceof \Pimf\Route\Target) { $controllerName = $target->getController(); } } if (Sapi::isCli() && Config::get('environment') == 'production') { $controllerName = $request->fromCli()->get('controller'); } if (!$controllerName) { $controllerName = Config::get('app.default_controller'); } $this->repositoryPath = $repositoryPath; $this->request = $request; $this->controllerClass = $prefix . 'Controller\\'; $basepath = $this->repositoryPath . '/'; $controller = ucfirst($controllerName); if (Str::isEvilPath($basepath . $controller)) { throw new Bomb('directory traversal attack is not funny!'); } $this->controllerPath = $basepath . $controller . '.php'; if (!file_exists($this->controllerPath)) { throw new Bomb('no "' . $controller . '" controller found at the repository path'); } }
/** * @param boolean $routeable * @param string $routes Path to routes definition file. */ private static function loadRoutes($routeable, $routes) { if ($routeable === true && file_exists($routes)) { self::$router = new Router(); foreach ((array) (include $routes) as $route) { self::$router->map($route); } } }