What's for:
<?php
class Request{
private $postData= array();
private $getData= array();
public function __construct(){
//Registry::set('request', $this);
$this->parseQuery();
}
public function getPost($key){
return isset($_POST[$key]) ? $_POST[$key] : null;
}
public function getGet($key){
return isset($this->getData[$key]) ? trim($this->getData[$key]) : null;
}
public function formHasPropriety(){
$hasProperty= true;
$args= func_get_args();
foreach($args as $arg){
if(is_null($value=$this->getPost($arg))){
$hasProperty= false;
}
else{
$this->postData[$arg]= trim($value);
}
}
return $hasProperty;
}
public function linkHasPropriety(){
$hasProperty= true;
$args= func_get_args();
foreach($args as $arg){
if(is_null($value=$this->getGet($arg))){
$hasProperty= false;
}
}
return $hasProperty;
}
public function getPostValue(){
return $this->postData;
}
public function getGetValue(){
return $this->getData;
}
private function parseQuery(){
$current= current($_GET);
if(!empty($current)){
parse_str($_SERVER['QUERY_STRING'],$this->getData);
}
}
public function redirect($page){
$host= $_SERVER['HTTP_HOST'];
$uri= rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra= $page;
header("Location: http://$host$uri/$extra");
exit();
}
public function refresh($page,$time=3){
$extra= $page;
header("Refresh: $time; URL=\"$extra\"");
}
final public function __destruct(){
unset($this->postData);
unset($this->getData);
}
}//
$request= new Request();
/* YOU MUST PUT THE POST REQUEST BEFORE */
if($request->formHasPropriety('title','check_insert','save')){
print_r($request->getPostValue());
}
elseif($request->formHasPropriety('title_','check_insert_','save_')){
print_r($request->getPostValue());
}
elseif($request->linkHasPropriety('y') && !$request->linkHasPropriety('m')){
echo "index?y=".$request->getGet('y');
}
elseif($request->linkHasPropriety('m','y') && !$request->linkHasPropriety('d')){
echo "index?m=".$request->getGet('m')."&y=".$request->getGet('y');
}
elseif($request->linkHasPropriety('d','m','y')){
echo "index?d=".$request->getGet('d')."&m=".$request->getGet('m')."&y=".$request->getGet('y');
}
elseif($request->linkHasPropriety('c')){
echo "index?c=".$request->getGet('c');
}
else{
echo "INDEX";
}
//var_dump($_POST);
?>
<br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=1">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?y= 2007">Browse by year</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?m=1&y=2007">Browse by month</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?d=5&m=1&y=2007">Browse by day</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?c=5">Browse by Category</a><br />
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="frm-tag">
<fieldset id="comment-1">
<label for="title">Comment : </label>
<input name="title" title="Comment name" type="text" value="" id="title-comment" /><br>
<input name="check_insert" type="hidden" value="1189086462" />
<label for="save"> </label>
<input name="save" type="submit" value="Save" id="save-comment" />
</fieldset>
</form>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="frm-tag">
<fieldset id="comment-2">
<label for="title">Comment : </label>
<input name="title_" title="Comment name" type="text" value="" id="title" /><br>
<input name="check_insert_" type="hidden" value="1189086462" />
<label for="save"> </label>
<input name="save_" type="submit" value="Save" id="save" />
</fieldset>
</form>
Just a simple controller 😉
Any comments on how I should be doing this better would be gratefully appreciated.