0

Hi all, I have taken from net MVC tutorial, but it is in German language. Can anyone explain it me why there were joined two superglobal? What is a purpose?
its below

<?php

// unsere Klassen einbinden
include('classes/controller.php');
include('classes/model.php');
include('classes/view.php');

// $_GET und $_POST zusammenfasen, $_COOKIE interessiert uns nicht.
$request = array_merge($_GET, $_POST);
// Controller erstellen
$controller = new Controller($request);
// Inhalt der Webanwendung ausgeben.
echo $controller->display();

?>

The all code i have attached

Edited by azegurb: n/a

Attachments
<?php
class Controller{

	private $request = null;
	private $template = '';

	/**
	 * Konstruktor, erstellet den Controller.
	 *
	 * @param Array $request Array aus $_GET & $_POST.
	 */
	public function __construct($request){
		$this->request = $request;
		$this->template = !empty($request['view']) ? $request['view'] : 'default';
	}

	/**
	 * Methode zum anzeigen des Contents.
	 *
	 * @return String Content der Applikation.
	 */
	public function display(){
		$view = new View();
		switch($this->template){
			case 'entry':
				$view->setTemplate('entry');
				$entryid = $this->request['id'];
				$entry = Model::getEntry($entryid);
				$view->assign('title', $entry['title']);
				$view->assign('content', $entry['content']);
				break;
				
			case 'default':
			default:
				$entries = Model::getEntries();
				$view->setTemplate('default');
				$view->assign('entries', $entries);
		}
		return $view->loadTemplate();
	}
}
?>
<h1>Willkommen im MVC-Blog!</h1>

<?php
foreach($this->_['entries'] as $entry){
?>

<h2><a href="?view=entry&id=<?php echo $entry['id'] ?>"><?php echo $entry['title']; ?></a></h2>
<p><?php echo $entry['content']; ?></p>

<?php
}
?>
<h1>Willkommen im MVC-Blog!</h1>
<h2><?php echo $this->_['title']; ?></h2>
<p><?php echo $this->_['content']; ?></p>
<a href="?view=deafult">Zur&uuml;ck zur &Uuml;bersicht</a>
<?php

// unsere Klassen einbinden
include('classes/controller.php');
include('classes/model.php');
include('classes/view.php');

// $_GET und $_POST zusammenfasen, $_COOKIE interessiert uns nicht.
$request = array_merge($_GET, $_POST);
// Controller erstellen
$controller = new Controller($request);
// Inhalt der Webanwendung ausgeben.
echo $controller->display();

?>
<?php
/**
 * Klasse fr den Datenzugriff
 */
class Model{

	//Eintrge eines Blogs als zweidimensionales Array
	private static $entries = array(
		array("id"=>0, "title"=>"Eintrag 1", "content"=>"Ich bin der erste Eintrag."),
		array("id"=>1, "title"=>"Eintrag 2", "content"=>"Ich bin der ewige Zweite!"),
		array("id"=>2, "title"=>"Eintrag 3", "content"=>"Na dann bin ich die Nummer drei.")
	);

	/**
	 * Gibt alle Eintrge des Blogs zurck.
	 *
	 * @return Array Array von Blogeintrgen.
	 */
	public static function getEntries(){
		return self::$entries;
	}

	/**
	 * Gibt einen bestimmten Eintrag zurck.
	 *
	 * @param int $id Id des gesuchten Eintrags
	 * @return Array Array, dass einen Eintrag reprsentiert, bzw. 
	 * 					wenn dieser nicht vorhanden ist, null.
	 */
	public static function getEntry($id){
		if(array_key_exists($id, self::$entries)){
			return self::$entries[$id];
		}else{
			return null;
		}
	}
}
?>
<?php
class View{

	// Pfad zum Template
	private $path = 'templates';
	// Name des Templates, in dem Fall das Standardtemplate.
	private $template = 'default';

	/**
	 * Enthlt die Variablen, die in das Template eingebetet
	 * werden sollen.
	 */
	private $_ = array();

	/**
	 * Ordnet eine Variable einem bestimmten Schl&uuml;ssel zu.
	 *
	 * @param String $key Schlssel
	 * @param String $value Variable
	 */
	public function assign($key, $value){
		$this->_[$key] = $value;
	}


	/**
	 * Setzt den Namen des Templates.
	 *
	 * @param String $template Name des Templates.
	 */
	public function setTemplate($template = 'default'){
		$this->template = $template;
	}


	/**
	 * Das Template-File laden und zurckgeben
	 *
	 * @param string $tpl Der Name des Template-Files (falls es nicht vorher 
	 * 						ber steTemplate() zugewiesen wurde).
	 * @return string Der Output des Templates.
	 */
	public function loadTemplate(){
		$tpl = $this->template;
		// Pfad zum Template erstellen & berprfen ob das Template existiert.
		$file = $this->path . DIRECTORY_SEPARATOR . $tpl . '.php';
		$exists = file_exists($file);

		if ($exists){
			// Der Output des Scripts wird n einen Buffer gespeichert, d.h.
			// nicht gleich ausgegeben.
			ob_start();

			// Das Template-File wird eingebunden und dessen Ausgabe in
			// $output gespeichert.
			include $file;
			$output = ob_get_contents();
			ob_end_clean();
				
			// Output zurckgeben.
			return $output;
		}
		else {
			// Template-File existiert nicht-> Fehlermeldung.
			return 'could not find template';
		}
	}
}
?>
4
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by diafol
0

that could be old?

All i know is $_REQUEST is a standard array like $_GET or $_POST, it's in php.ini which overwrites the other

$_GET;//get1
$_GET;//empty
$_GET;//empty

$_POST;//post1
$_POST;//post2
$_POST;//empty

//depending on php.ini
$_REQUEST;//post1
$_REQUEST;//post2
$_REQUEST;//empty

$_REQUEST;//get1
$_REQUEST;//post2
$_REQUEST;//empty


The only reason i can think of to do that is if you could get data coming in in both arrays that could over-write each other like above and you want to keep it. But even so array_merge is pointless cause it will overwrite it just like the $_REQUEST global!

0

Can you me simple example for this (array_merge($_GET, $_POST)) if possible?

0
$array1 = array('apple'=>25,'orange'=>35);
$array2 = array('banana'=>40,'pineapple'=>80,'tomato'=>50,'orange'=>55);
$array3 = array_merge($array1,$array2);

$array4 = array(5,4,8,3,9);
$array5 = array(1,7,5,6,4);
$array6 = array_merge($array4,$array5);

echo count($array1).'<br/>';
var_dump($array1);
echo '<br/>';
echo count($array2).'<br/>';
var_dump($array2);
echo '<br/>';
echo count($array3).'<br/>';
var_dump($array3);
echo '<br/>';
echo 'orange overwritten';
echo '<br/>';
echo count($array4).'<br/>';
var_dump($array4);
echo '<br/>';
echo count($array5).'<br/>';
var_dump($array5);
echo '<br/>';
echo count($array6).'<br/>';
var_dump($array6);
echo '<br/>';
echo 'arrays with integar indexes added as new entry';
0

Biiim,
I would like know what principles serve array_merge($_GET, $_POST)
i would like to learn its working principle only joining with $_GET and $_POST methods

0

I'm not quite sure what you're asking, if you're asking for the philosophy of life for a php function and 2 global arrays im not sure there is one, array_merge joins 2 arrays together, the $_GET array takes variables from a GET request(variables in the url eg page.php?id=5&new=true), $_POST takes variables in from a POST request which is sent in the HTTP request headers (id=5&new=true).

Very similar things GET holds much less data and is visible in the address bar and history etc and POST can hold much more data (which can be set in the php.ini file its default is 1kb) and is much more hidden client side.

If you're asking for a real world example, i have never joined together the post and get arrays. I use POST for receiving user submitted data and GET is generally for viewing already existing data.
eg.
addproduct.php would use POST
viewproduct.php?id=5 would use GET

On a page where you could both view and submit data such as a forum/blog comment - the thread id in the url(GET) and your comment/post(POST) even then you know which is GET and which is POST so there is still no need to join them the id is usually passed into a hidden input field so even the thread id is POST too.

The only time i can imagine using a post & get merge is for ease of use such as a function/app written for other php developers where people can just throw their data at it however they like, post or get, and it pulls the data and runs, much like the files you first posted

1

array merges combines an array based on the keys. so if you have $_GET = 'foo' and $_POST = 'bar'... easier said in an example

$_GET['test1'] = 'foo';
$_POST['test1'] = 'bar';

$array = array_merge($_GET['test1'], $_POST['test1']);

//$array['test1'] becomes bar

//here's how I usually use array_merge

class foo{
    
    protected $_config = array();
    
    public function __construct ( array $array ) {
        $default = array(/*default options*/);
        $this->_config = array_merge($default, $array);
    }
}

kinda like setting options in a plugin and having defaults as a fallback

1

You can pass data with get and post with a posted form:

<form action="page.php?id=12&colour=red" method="post">
<input name="year" type="text" /> <!-- e.g. 1978 -->
...
</form>

I tend to use one OR the other, but neither is deemed 'safe' - the validation and sanitization process for any data input is key.

Using $_REQUEST is bad. I can only imagine that the CMS tries to emulate a safer version of this superglobal, but without the cookie aspect.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.