Maybe I'm too blind to see it, but how am I supposed to access the parameters submitted via
http://[host]/index.php/[controller]/[action]/[param1]/[param2]/[param3]...
in my Controller?
The first thing I did was hacking
TinyMVC.php to pass a previously created array of Parameters seems nice at first but I don't really like it. You can only access your parameters by the order of appearance in the request string. There's no way to name them and it involves hacking which I don't like in the first place.
So I created a plugin
library.Parameters.class to do the work for me.
It does the following thing: It writes all the passed Parameters into an internal array. Parameters, that have a '=' sign will be split up into
parameter name = parameter valueIt also loads $_GET and $_POST into the internal array. I'm not sure if I really like that point, maybe I'll find some smoother way to handle those superglobals in my Plugin.
It provides a __get() method so parameters that have been passed this way
http://localhost/members/show/id=1can be accessed this way
$this->param->id;in the Controller.
Here's the code:
<?php
/**
* library.parameters.php
* Plugin for Tiny_MVC to provide information about submitted parameters on to the controllers
*
* @package TinyMVC
* @author Daniel Gaberszig
*/
class Parameters {
/**
* $_params
*
* Internal list of gathered parameters
*
* @access private
*/
var $_params = array();
/**
* class constructor
*
* @access public
*/
function __construct() {
// Get the path information
$path_info = !empty($_SERVER['PATH_INFO']) ? explode('/',$_SERVER['PATH_INFO']) : null;
/* $path_info holds the following values:
* [0] => ???
* [1] => controller name
* [2] => action name
* [3]...[n] => parameters
*
* Every parameter will we written into the $_params array. If a parameter
* looks like .*=.* the left side of the = will be handled as array key
* and the right side will be handled as array value
*/
for($i = 3, $cnt = count($path_info);$i < $cnt; $i++) {
// Get the current parameter
$param = $path_info[$i];
// Look for an '='
$pos = strpos($param, '=');
if ($pos > 0) {
// Found -> split the parameter up
$param_name = substr($param, 0, $pos);
$param = substr($param, $pos+1);
$this->_params[$param_name] = $param;
} else
// Not found -> assign the parameter
$this->_params[] = $param;
}
// Get all variables passed via GET into the $_params array
foreach($_GET as $key => $val) {
$this->_params[$key] = $val;
}
// Get all variables passed via POST into the $_params array
foreach($_POST as $key => $val) {
$this->_params[$key] = $val;
}
} // function __construct()
/**
* __get
*
* Make a parameter named 'id' will accessible via the
* following syntax in the controller:
* $this->params->id
*
* @access public
* @param string $key The key we are trying to access
* @return mixed The value of the parameter or null if no such key exists
*/
function __get($key) {
return $this->get($key);
}
/**
* get
*
* Get the value of a parameter
*
* @access public
* @param string $key The key we are trying to access
* @return mixed The value of the parameter or null if no such key exists
*/
function get($key) {
return array_key_exists($key, $this->_params) ?
$this->_params[$key] : null;
}
/**
* get_all
*
* Get all parameters
*
* @access public
* @return array An array of all parameters
*/
function get_all() {
return $this->_params;
}
} // class Parameters
?>(( Is there any other way of creating a box with syntax highlighted code than [code ]<?php ...[/code ]? ))