Example #1
0
 /**
  * Get the base URL of the website that is visited on the framework. Also includes the protocol 
  * for HTTP and HTTPS.
  * ```
  * // Example: 
  * URL::base()
  * ```
  * @uses Stativo\Core\Request
  * @return string FQDN with protocol http://example.com/
  */
 public static function base()
 {
     // check for https
     if (Request::instance()->server('HTTPS') != NULL) {
         $protocol = Request::instance()->server('HTTPS') == 'on' ? 'https' : 'http';
     } else {
         $protocol = Request::instance()->server('SERVER_PORT') == '443' ? 'https' : 'http';
     }
     return $protocol . '://' . Request::instance()->server('HTTP_HOST') . '/';
 }
Example #2
0
 /**
  * Generates an opening HTML form tag.
  *
  * @param   string  $action form action
  * @param   string  $method Method GET|POST|PUT|DELETE
  * @param   array   $attributes html attributes
  * @return  string
  */
 public static function open($action = NULL, $method = 'POST', array $attributes = NULL)
 {
     if ($action === NULL) {
         // Use the current URI
         $action = '/' . Request::instance()->uri();
     }
     if ($action === '') {
         // Use only the base URI
         $action = URL::base();
     } elseif (strpos($action, '://') === FALSE) {
         // Make the URI absolute
         $action = URL::site($action);
     }
     // Add the form action to the attributes
     $attributes['action'] = $action;
     // Form method is always POST
     $attributes['method'] = 'post';
     return '<form' . HTML::attributes($attributes) . '>' . PHP_EOL . static::hidden('__method', $method);
 }
Example #3
0
 /**
  * Instance of Middleware
  */
 public function __construct()
 {
     self::$request = Request::instance();
     self::$response = Response::instance();
     return $this;
 }
Example #4
0
 /**
  * Will execute the called request, also checks if the request
  * parameters are correct (like: Method and Middleware)
  *
  * @uses Stativo\Core\Middleware
  * @uses Stativo\Core\Route
  * @uses Stativo\Helpers\HttpException
  * @uses Stativo\Helpers\MiddlewareException
  * @return this
  */
 public function execute()
 {
     self::$_server = $_SERVER;
     self::$_get = $_GET;
     self::$_post = $_POST;
     self::$_put = $_POST;
     self::$_delete = $_POST;
     // Check if HTTP methods are set
     if (isset(self::$_post['__method'])) {
         // if __method isset, then use them
         if (strtolower(self::$_post['__method']) == strtolower(Route::PUT) or strtolower(self::$_post['__method']) == strtolower(Route::DELETE)) {
             self::$_method = self::$_post['__method'];
         } else {
             // Use default method
             self::$_method = $_SERVER['REQUEST_METHOD'];
         }
     } else {
         // Use default method
         self::$_method = $_SERVER['REQUEST_METHOD'];
     }
     unset(self::$_post['__method']);
     unset(self::$_put['__method']);
     unset(self::$_delete['__method']);
     // Match URI's in Stativo\Core\Route
     $match = $this->process_uri(Request::instance());
     // Check matches
     if ($match === null) {
         // Throw Stativo\Helpers\HttpException (404)
         throw new HttpException(404, 'URI /:uri is not found!', array(':uri' => $this->uri()));
     } else {
         // Match successful!
         $controller = '';
         $action = 'index';
         if (isset($match['params']['directory'])) {
             $controller .= str_replace('/', '\\', $match['params']['directory'] . '/');
             unset($match['params']['directory']);
         }
         if (isset($match['params']['controller'])) {
             $controller .= $match['params']['controller'];
             unset($match['params']['controller']);
         }
         if (isset($match['params']['action'])) {
             $action = $match['params']['action'];
             unset($match['params']['action']);
         }
         // Get current route
         $route = Route::current();
         // Check methods, if they compare then launch class and action
         if ($route->_method === Route::ANY or in_array(self::$_method, !is_array($route->_method) ? array($route->_method) : $route->_method)) {
             if ($middleware = Middleware::boot($route)) {
                 self::$_params = $match['params'];
                 // Find required controller
                 $controller = File::find($controller, 'Controller');
                 // Call classes
                 $class = new $controller();
                 $class->{$action}();
                 \Stativo\Helpers\Power::runOff();
             } else {
                 throw new MiddlewareException("':middleware' encountered an error!", [':middleware' => Middleware::$name]);
             }
         } else {
             // Throw Stativo\Helpers\HttpException, method mismatch
             throw new HttpException(500, 'Route method mismatch, Expected: :method request', [':method' => is_array($route->_method) ? implode(', ', $route->_method) : $route->_method]);
         }
     }
     return Response::instance();
 }
Example #5
0
 /**
  * Controller handler. This class should be extended
  * by every controller that is callable trough a route
  *
  * @uses Stativo\Core\Request
  * @uses Stativo\Core\Response
  */
 public function __construct()
 {
     $this->request = Request::instance();
     $this->response = Response::instance();
 }
Example #6
0
 /**
  * Tests if the route matches a given Request. A successful match will return
  * all of the routed parameters as an array. A failed match will return
  * boolean FALSE.
  *
  *     // Params: controller = users, action = edit, id = 10
  *     $params = $route->matches(Request::factory('users/edit/10'));
  *
  * This method should almost always be used within an if/else block:
  *
  *     if ($params = $route->matches($request))
  *     {
  *         // Parse the parameters
  *     }
  *
  * @param   Request $request  Request object to match
  * @return  array             on success
  * @return  FALSE             on failure
  */
 public function matches(Request $request, $name)
 {
     // Get the URI from the Request
     $_request = Request::instance();
     $uri = $_request->uri();
     if (!preg_match($this->_route_regex, $uri, $matches)) {
         return FALSE;
     }
     $params = array();
     foreach ($matches as $key => $value) {
         if (is_int($key)) {
             // Skip all unnamed keys
             continue;
         }
         // Set the value for all matched keys
         $params[$key] = $value;
     }
     foreach ($this->_defaults as $key => $value) {
         if (!isset($params[$key]) or $params[$key] === '') {
             // Set default values for any key that was not matched
             $params[$key] = $value;
         }
     }
     if (!empty($params['controller'])) {
         // PSR-0: Replace underscores with spaces, run ucwords, then replace underscore
         $params['controller'] = str_replace(' ', '\\', ucwords(str_replace('\\', ' ', $params['controller'])));
     }
     if (!empty($params['directory'])) {
         // PSR-0: Replace underscores with spaces, run ucwords, then replace underscore
         $params['directory'] = str_replace(' ', '\\', ucwords(str_replace('\\', ' ', $params['directory'])));
     }
     if ($this->_filters) {
         foreach ($this->_filters as $callback) {
             // Execute the filter giving it the route, params, and request
             $return = call_user_func($callback, $this, $params, $request);
             if ($return === FALSE) {
                 // Filter has aborted the match
                 return FALSE;
             } elseif (is_array($return)) {
                 // Filter has modified the parameters
                 $params = $return;
             }
         }
     }
     self::$_current_route = Route::get($name);
     return $params;
 }