Exemplo n.º 1
0
 /**
  * Parses givven url, tries to match it against routes
  *
  * @param  string $url
  * @return array  Current route
  */
 public function parse_url($url)
 {
     $out = [];
     $ext = null;
     if (strpos($url, '/') !== 0) {
         $url = '/' . $url;
     }
     foreach ($this->routes as $route => $params) {
         if (($matches = $this->match_route($route, $url)) !== false) {
             $names = $params['names'];
             $defaults = $params['defaults'];
             array_shift($matches);
             foreach ($matches as $key => $found) {
                 if (empty($found) && $found !== '0' && $found !== 0) {
                     continue;
                 }
                 if (isset($names[$key])) {
                     $out[$names[$key]] = $found;
                 } else {
                     foreach (array_cleanup(explode('/', $found)) as $param) {
                         array_push($out, $param);
                     }
                 }
             }
             $out = array_merge($defaults, $out);
             break;
         }
     }
     self::$current_route = $out;
     return $out;
 }
Exemplo n.º 2
0
Arquivo: base.php Projeto: brego/prank
/**
 * Returns the filename with a relative path to the root of the page
 *
 * url([controller => controller_name]) // Assumes action = index
 * url([controller => controller_name, action => action_name])
 * url(action => action_name]) // Assumes current controller
 * url([controller => controller_name, action => action_name, param, param2])
 *
 * url('/controller/action')
 * url('action') // Assumes current controller, no '/' at first character
 *
 * @param  mixed $path A file or action/controller/param or an array
 * @return string Relative path to the file
 * @todo   This is far from being rock-solid... Could use some rethinking or
 *         refactoring...
 * @todo   Improve documentation
 **/
function url($path)
{
    $controller = Registry::instance()->current_controller;
    if (is_array($path)) {
        if (isset($path['controller']) && isset($path['action']) === false) {
            $path['action'] = 'index';
        } elseif (isset($path['action']) && isset($path['controller']) === false) {
            $path['controller'] = $controller->get_controller();
        } elseif (isset($path['controller']) === false && isset($path['action']) === false) {
            $path['action'] = $controller->action;
            $path['controller'] = $controller->get_controller();
        }
        $result = [$path['controller'], $path['action']];
        unset($path['controller'], $path['action']);
        if (isset($path['id']) === true) {
            $result[] = $path['id'];
            unset($path['id']);
        }
        if (count($path) > 0) {
            $result = array_merge($result, $path);
        }
        $path = implode('/', $result);
    } else {
        if ($path[0] !== '/') {
            $path = $controller->get_controller() . '/' . $path;
        } else {
            $path = substr($path, 1);
        }
    }
    if (isset($_SERVER['REQUEST_URI'])) {
        $request = explode('/', $_SERVER['REQUEST_URI']);
        $request = array_cleanup($request);
        $url = [];
        if (isset($_GET['url'])) {
            $url = explode('/', $_GET['url']);
            $url = array_cleanup($url);
        }
        $result = array_diff($request, $url);
        return '/' . implode('/', $result) . '/' . $path;
    } else {
        return '/' . $path;
    }
}
Exemplo n.º 3
0
Arquivo: html.php Projeto: brego/prank
/**
 * HTML helper
 *
 * @filesource
 * @copyright  Copyright (c) 2008-2014, Kamil "Brego" Dzieliński
 * @license    http://opensource.org/licenses/mit-license.php The MIT License
 * @author     Kamil "Brego" Dzieliński <*****@*****.**>
 * @link       http://prank.brego.dk/ Prank's project page
 * @link       http://github.com/brego/prank/ Prank's Git repository
 * @package    Prank
 * @subpackage Helpers
 * @since      Prank 0.25
 * @version    Prank 0.75
 */
function is_current_page($path)
{
    $controller = Registry::instance()->current_controller;
    if (is_string($path)) {
        $result = [];
        if ($path[0] === '/') {
            $result['controller'] = true;
        }
        $path = explode('/', $path);
        $path = array_cleanup($path);
        if (isset($result['controller']) && isset($path[0])) {
            $result['controller'] = $path[0];
            if (isset($path[1])) {
                $result['action'] = $path[1];
            }
        } elseif (isset($path[0])) {
            $result['action'] = $path[0];
        } else {
            return false;
        }
        $path = $result;
    }
    if (isset($path['controller']) === true && $path['controller'] === $controller->get_controller()) {
        if (isset($path['action']) === true) {
            if ($path['action'] === $controller->action) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    } elseif (isset($path['action']) === true && $path['action'] === $controller->action) {
        return true;
    } else {
        return false;
    }
}