Skip to content

Instantly share code, notes, and snippets.

@williankeller
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save williankeller/660f86bb0be998137157 to your computer and use it in GitHub Desktop.

Select an option

Save williankeller/660f86bb0be998137157 to your computer and use it in GitHub Desktop.
/**
* Verifica chaves de arrays
*
* Verifica se a chave existe no array e se ela tem algum valor.
* Obs.: Essa função está no escopo global, pois, vamos precisar muito da mesma.
*
* @param array $array O array
* @param string $key A chave do array
* @return string|null O valor da chave do array ou nulo
*/
function chk_array($array, $key) {
// Verifica se a chave existe no array
if (isset($array[$key]) && !empty($array[$key])) {
// Retorna o valor da chave
return $array[$key];
}
// Retorna nulo por padrão
return null;
}
/* ------------------------- */
/**
* Obtém parâmetros de $_GET['path']
*
* Obtém os parâmetros de $_GET['path'] e configura as propriedades
* $this->controlador, $this->acao e $this->parametros
*
* A URL deverá ter o seguinte formato:
* http://agenciahome.com.br/controlador/acao/parametro1/parametro2/etc...
*/
public function get_url_data() {
// Verifica se o parâmetro path foi enviado
if (isset($_GET['path'])) {
// Captura o valor de $_GET['path']
$path = $_GET['path'];
// Limpa os dados
$path = rtrim($path, '/');
$path = filter_var($path, FILTER_SANITIZE_URL);
// Cria um array de parâmetros
$path = explode('/', $path);
// Configura as propriedades
$this->controlador = chk_array($path, 0);
$this->controlador .= '.controller';
$this->acao = chk_array($path, 1);
// Configura os parâmetros
if (chk_array($path, 2)) {
unset($path[0]);
unset($path[1]);
// Os parâmetros sempre virão após a ação
$this->parametros = array_values($path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment