Helpers refactoring
This commit is contained in:
115
app/Helper/Url.php
Normal file
115
app/Helper/Url.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Helper;
|
||||
|
||||
use Core\Request;
|
||||
use Core\Security;
|
||||
|
||||
/**
|
||||
* Url helpers
|
||||
*
|
||||
* @package helper
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Url extends \Core\Base
|
||||
{
|
||||
/**
|
||||
* HTML Link tag
|
||||
*
|
||||
* @access public
|
||||
* @param string $label Link label
|
||||
* @param string $controller Controller name
|
||||
* @param string $action Action name
|
||||
* @param array $params Url parameters
|
||||
* @param boolean $csrf Add a CSRF token
|
||||
* @param string $class CSS class attribute
|
||||
* @param boolean $new_tab Open the link in a new tab
|
||||
* @return string
|
||||
*/
|
||||
public function link($label, $controller, $action, array $params = array(), $csrf = false, $class = '', $title = '', $new_tab = false)
|
||||
{
|
||||
return '<a href="'.$this->href($controller, $action, $params, $csrf).'" class="'.$class.'" title="'.$title.'" '.($new_tab ? 'target="_blank"' : '').'>'.$label.'</a>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Hyperlink
|
||||
*
|
||||
* @access public
|
||||
* @param string $controller Controller name
|
||||
* @param string $action Action name
|
||||
* @param array $params Url parameters
|
||||
* @param boolean $csrf Add a CSRF token
|
||||
* @return string
|
||||
*/
|
||||
public function href($controller, $action, array $params = array(), $csrf = false)
|
||||
{
|
||||
$values = array(
|
||||
'controller' => $controller,
|
||||
'action' => $action,
|
||||
);
|
||||
|
||||
if ($csrf) {
|
||||
$params['csrf_token'] = Security::getCSRFToken();
|
||||
}
|
||||
|
||||
$values += $params;
|
||||
|
||||
return '?'.http_build_query($values, '', '&');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate controller/action url
|
||||
*
|
||||
* @access public
|
||||
* @param string $controller Controller name
|
||||
* @param string $action Action name
|
||||
* @param array $params Url parameters
|
||||
* @return string
|
||||
*/
|
||||
public function to($controller, $action, array $params = array())
|
||||
{
|
||||
$values = array(
|
||||
'controller' => $controller,
|
||||
'action' => $action,
|
||||
);
|
||||
|
||||
$values += $params;
|
||||
|
||||
return '?'.http_build_query($values, '', '&');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get application base url
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function base()
|
||||
{
|
||||
$application_url = $this->config->get('application_url');
|
||||
|
||||
if (! empty($application_url)) {
|
||||
return $application_url;
|
||||
}
|
||||
|
||||
return $this->server();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current server base url
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function server()
|
||||
{
|
||||
$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.'/' : '/';
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user