Start to implement url rewrite (nice urls)
This commit is contained in:
@@ -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, '', '&').(empty($anchor) ? '' : '#'.$anchor);
|
||||
return $this->build('&', $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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user