Start to implement url rewrite (nice urls)

This commit is contained in:
Frederic Guillot
2015-07-12 10:38:42 -04:00
parent 7e94d0ca23
commit bc98748c0d
35 changed files with 547 additions and 208 deletions

View File

@@ -18,7 +18,7 @@ class Asset extends \Core\Base
*/
public function js($filename, $async = false)
{
return '<script '.($async ? 'async' : '').' type="text/javascript" src="'.$filename.'?'.filemtime($filename).'"></script>';
return '<script '.($async ? 'async' : '').' type="text/javascript" src="'.$this->helper->url->dir().$filename.'?'.filemtime($filename).'"></script>';
}
/**
@@ -31,7 +31,7 @@ class Asset extends \Core\Base
*/
public function css($filename, $is_file = true, $media = 'screen')
{
return '<link rel="stylesheet" href="'.$filename.($is_file ? '?'.filemtime($filename) : '').'" media="'.$media.'">';
return '<link rel="stylesheet" href="'.$this->helper->url->dir().$filename.($is_file ? '?'.filemtime($filename) : '').'" media="'.$media.'">';
}
/**

View File

@@ -13,6 +13,9 @@ use Core\Security;
*/
class Url extends \Core\Base
{
private $base = '';
private $directory = '';
/**
* HTML Link tag
*
@@ -33,7 +36,7 @@ class Url extends \Core\Base
}
/**
* Hyperlink
* HTML Hyperlink
*
* @access public
* @param string $controller Controller name
@@ -41,22 +44,12 @@ class Url extends \Core\Base
* @param array $params Url parameters
* @param boolean $csrf Add a CSRF token
* @param string $anchor Link Anchor
* @param boolean $absolute Absolute or relative link
* @return string
*/
public function href($controller, $action, array $params = array(), $csrf = false, $anchor = '')
public function href($controller, $action, array $params = array(), $csrf = false, $anchor = '', $absolute = false)
{
$values = array(
'controller' => $controller,
'action' => $action,
);
if ($csrf) {
$params['csrf_token'] = Security::getCSRFToken();
}
$values += $params;
return '?'.http_build_query($values, '', '&amp;').(empty($anchor) ? '' : '#'.$anchor);
return $this->build('&amp;', $controller, $action, $params, $csrf, $anchor, $absolute);
}
/**
@@ -66,18 +59,13 @@ class Url extends \Core\Base
* @param string $controller Controller name
* @param string $action Action name
* @param array $params Url parameters
* @param string $anchor Link Anchor
* @param boolean $absolute Absolute or relative link
* @return string
*/
public function to($controller, $action, array $params = array())
public function to($controller, $action, array $params = array(), $anchor = '', $absolute = false)
{
$values = array(
'controller' => $controller,
'action' => $action,
);
$values += $params;
return '?'.http_build_query($values, '', '&');
return $this->build('&', $controller, $action, $params, false, $anchor, $absolute);
}
/**
@@ -88,7 +76,28 @@ class Url extends \Core\Base
*/
public function base()
{
return $this->config->get('application_url') ?: $this->server();
if (empty($this->base)) {
$this->base = $this->config->get('application_url') ?: $this->server();
}
return $this->base;
}
/**
* Get application base directory
*
* @access public
* @return string
*/
public function dir()
{
if (empty($this->directory) && isset($_SERVER['REQUEST_METHOD'])) {
$this->directory = str_replace('\\', '/', dirname($_SERVER['PHP_SELF']));
$this->directory = $this->directory !== '/' ? $this->directory.'/' : '/';
$this->directory = str_replace('//', '/', $this->directory);
}
return $this->directory;
}
/**
@@ -103,13 +112,46 @@ class Url extends \Core\Base
return 'http://localhost/';
}
$self = str_replace('\\', '/', dirname($_SERVER['PHP_SELF']));
$url = Request::isHTTPS() ? 'https://' : 'http://';
$url .= $_SERVER['SERVER_NAME'];
$url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 ? '' : ':'.$_SERVER['SERVER_PORT'];
$url .= $self !== '/' ? $self.'/' : '/';
$url .= $this->dir() ?: '/';
return $url;
}
/**
* Build relative url
*
* @access private
* @param string $separator Querystring argument separator
* @param string $controller Controller name
* @param string $action Action name
* @param array $params Url parameters
* @param boolean $csrf Add a CSRF token
* @param string $anchor Link Anchor
* @param boolean $absolute Absolute or relative link
* @return string
*/
private function build($separator, $controller, $action, array $params = array(), $csrf = false, $anchor = '', $absolute = false)
{
$path = $this->router->findUrl($controller, $action, $params);
$qs = array();
if (empty($path)) {
$qs['controller'] = $controller;
$qs['action'] = $action;
$qs += $params;
}
if ($csrf) {
$qs['csrf_token'] = Security::getCSRFToken();
}
if (! empty($qs)) {
$path .= '?'.http_build_query($qs, '', $separator);
}
return ($absolute ? $this->base() : $this->dir()).$path.(empty($anchor) ? '' : '#'.$anchor);
}
}