Who should manage if there are the required/valid (URL) parameters in MVC/Zend Framework
For example, if my controller action expects an integer "id" parameter, what should ensure that requirement is met?
Maybe in the Controller Action do something like the below?
if (isset($this->_getParam("id")) && is_numeric($this->_getParam("id"))) { ... }
Then what about whether the id is vali开发者_StackOverflow社区d? eg. the row does not exists in the database?
simply by using
$validator = new Zend_Validate_Digits();
$id = $this->_getParam("id") ;
$valid = $validator->isValid($id);
if($valid){
// its valid number
}else{
// its not valid number
}
and if the id is exist or not exist in the db , there is some validator this example uses zend_db , you can write your custom validator
//Check that the email address exists in the database
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => 'users',
'field' => 'emailaddress'
)
);
if ($validator->isValid($emailaddress)) {
// email address appears to be valid
} else {
// email address is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}
These are two steps...
Validation of the given Data like you did or via form validation
Checking for a corresponding row in the DB via SQL or Model instanstiation
$row = $db->fetchRow($db->select()->where('ID= ?', $id));
echo count($row); // check count
echo !empty($row); // untested ...should work to
In theory, your controllers should be able to handle any data that comes via the url. Typically, I validate the relevance of the data (ie, is it a legit database id, name, command, etc?) before I do anything with it. I've never really felt the need for baseline type validation on url arguments. Testing for NULLs is certainly a good idea, although getParam
does allow you to specify a default should the value be null.
Anyway, here's how I would do this if I had to. Note: I haven't actually tested this code. It's all 'in theory'.
I would pass type information into the defaults
array of my route object.
// assuming you're using an ini config for routes
routes.myroute.defaults.arg_types.id[] = 'integer'
// so your 'defaults' array will looks like:
Array(
[controller] => mycontroller
[action] => myactions
[arg_types] => Array
(
[id] => Array
(
[0] => notnull
[1] => integer
)
)
)
Then I would write a Controller plugin that validates each argument based on the config data we've specified during the dispatch loop:
public function preDispatch($request)
{
$params = $request->getParams();
$route = Zend_Controller_Front::getInstance()->getRouter()->getCurrentRoute();
$type_args = $route->getDefault('type_args');
foreach($params as $param) {
if (!in_array($param, $type_args)) {
continue;
}
foreach($type_args[$param] as $type) {
switch($type) {
case 'notnull':
// validate for null
break;
case 'integer':
// validate for integer
break;
// etc.
}
}
}
}
Obviously, there is quite a bit more logic to be handled here, such as what to do when things don't validate.
精彩评论