/** * Run one of the mapped callbacks to a passed HTTP Method. * @param array $args The arguments to be passed to the callback * @param string $method The HTTP Method requested. * @return array The callback response. */ public function run(array $args, $method = 'get') { $method = strtolower($method); if (Request::method() !== $method) { return false; } // Call direct middlewares if ($this->middlewares) { // Reverse middlewares order foreach (array_reverse($this->middlewares) as $mw) { $mw_result = call_user_func($mw->bindTo($this)); if (false === $mw_result) { return ['']; } } } $callback = $this->callback; if (is_array($this->callback)) { if (array_key_exists($method, $this->callback)) { $callback = $this->callback[$method]; } } Event::trigger('core.route.before', $this); // Start capturing output Response::start(); $this->response = ''; $view_results = call_user_func_array($callback->bindTo($this), $args); // Render View if returned, else echo string or encode json response if (null !== $view_results) { if (is_a($view_results, 'View') || is_string($view_results)) { echo $this->response = (string) $view_results; } else { Response::json($view_results); } } Response::end(); // Apply afters if ($this->afters) { // Reverse middlewares order foreach (array_reverse($this->afters) as $cb) { call_user_func($cb->bindTo($this)); } } Event::trigger('core.route.after', $this); return [$this->response]; }
<?php // loosewire ajax backend require 'functions.php'; // settings Register::$base = __FILE__; Register::$upload_folder = 'uploads'; Register::$project_folder = 'projects'; // init register Register::init(); // start response Response::start(true); // true for debug // upload project if (Request::is_type('upload')) { if ($result = Project::upload()) { Response::exit_with($result); } else { Response::exit_with(['error' => 'Error uploading file']); } } // list projects if (Request::is_type('list')) { if ($result = Project::getlist()) { Response::exit_with($result); } else { Response::exit_with(['error' => 'Error fetching projects']); } } // list projects if (Request::is_type('load', ['filename'])) {