热搜:NVER node 开发 php

讨论一下mvc的C和V的维护性和健壮复用性

2024-08-13 10:40:01
讨论一下mvc的C和V的维护性和健壮复用性

mvc大家都很熟, 基本的结构也很明白。

今天我想和大家讨论一下,
其中控制层中,一个控制器里有多少个方法为合适,
以及,一个显示层是否可以多次重复调用(像增加用户页面和修改用户页面基本上是一样的)。

如果增加和修改用一个页面的话,势必会在view中写过多的逻辑判断,判断是增加控制层进来的,还是修改层进来的,以及,做完增加或修改后跳到哪个相应的控制层。 (这些都要在显示层给个action的值,或name属性,又或都get或post提交 这些判断来传到不同的控制层。)

因为在维护一个旧系统,感到很困惑。
给大家贴段代码看看以前旧项目的代码,哪需要提高,因为每次进到一个function中,都要不断的分析这个function是做什么的,维护起来,读代码的时间太浪费。请大家不吝赐教。

举例:控制层  //写的是修改成员 但里面有些功能不是修改成员数据,这只是一个文件,项目中其它地方的代码也都类似,甚至更多,每次进到方法里要走一遍流程才知道到底执行哪个,以及在哪跳出。 甚至跳出的地方用一个控制器的名称代替view(XXX.php) ,从而重复上边我说的,进入到又一次循环分析代码中。  维护起来很不方便。 需要怎么理这思路,以及以后注意哪方面进行提高 ,希望提些建设性的意见,谢谢。

//----------------修改成员数据-------------------------------		public function alterInfo()	{		$strAction = "";		$teamname=$_SESSION['teamname'];		if ($_GET){			$inUserid = $_GET['id'];				$data['userid'] = $inUserid;			}		if($_POST){			// var_dump($_POST);			// die();			$userid = $_POST['userid'];			$mobile = $_POST['mobile'];			$realname = $_POST['realname'];			$zhengjian = trim($this->input->post('zhengjian'));			//$idnumber = $_POST['idnumber'];			$idnumber = trim($this->input->post('idnu'));			$address = $_POST['address'];			$zipcode = $_POST['zipcode'];						$clothsize = $_POST['clothsize'];			$data['strClothOpt'] = $this->Common_model->getClothOpy($clothsize);			$birthday = trim($this->input->post('birthday')); 						$passportnu = trim($this->input->post('passportnu'));			$gender = trim($this->input->post('gender'));			if ($_FILES["idnumscan"]["error"] > 0)			  {			  	$data['error'] = $_FILES["idnumscan"]["error"] . "
"; $userpic = $_POST['idnumscans']; } else { $data['filename'] = $_FILES["idnumscan"]["name"]; $data['filetype'] = $_FILES["idnumscan"]["type"]; $data['filesize'] = ($_FILES["idnumscan"]["size"] / 1024) . " Kb"; $data['filepath'] = $_FILES["idnumscan"]["tmp_name"]; $excelname=iconv("UTF-8","gb2312", $_FILES["idnumscan"]["name"]); $userpic = $_FILES["idnumscan"]["name"]; move_uploaded_file($_FILES["idnumscan"]["tmp_name"],"upload/image/".$excelname); } if($_POST['action']=="add"){ $strAction = $teamname; $data['userid'] = ""; //倒数第二位是奇数为男性,偶数为女性 $gender = substr($idnumber,16,1)%2; //var_dump($gender); $teamid = $_SESSION['clubid']; $usertype=0; $this->load->model('Member_model'); $rowIsReg = $this->Member_model->isRegistered($idnumber,$clubid); if($rowIsReg){ echo ""; //redirect(base_url()."member"); }else{ $res = $this->Member_model->insMember($mobile, $realname, $idnumber, $gender, $address, $zipcode, $clubid, $usertype,$userpic,$clothsize); if($res){ echo ""; redirect(base_url()."team"); } } } if($_POST['action']=="alert"){ // var_dump($zhengjian); // die(); $strAction = $teamname; $data['userid'] = $userid; if($idnumber!='' and $zhengjian =='1'){ //用身份证增加或修改的 //倒数第二位是奇数为男性,偶数为女性 $gender = substr($idnumber,16,1)%2; //var_dump($gender); $clubid = $_SESSION['clubid']; $usertype=0; $this->load->model('Member_model'); $rowIsReg = $this->Member_model->isRegisteredtoo($idnumber,$clubid,$userid); if($rowIsReg){ echo ""; //redirect(base_url()."member"); }else{ $res = $this->Member_model->alertMember($mobile, $realname, $idnumber, $gender, $address, $zipcode, $clubid, $usertype,$userpic,$clothsize,$userid,$birthday,$passportnu); if($res){ echo ""; redirect(base_url()."team"); } } }else{ //用护照增加或修改的 // echo "111111111111"; // die(); $clubid = $_SESSION['clubid']; $usertype=0; $res = $this->Member_model->alertMemberpassportnu($mobile, $realname, $idnumber, $gender, $address, $zipcode, $clubid, $usertype,$userpic,$clothsize,$userid,$birthday,$passportnu,$gender); if($res){ echo ""; redirect(base_url()."team"); } } } $arrMember['userid'] = $userid; $arrMember['mobile'] = $mobile; $arrMember['realname'] = $realname; $arrMember['idnumber'] = $idnumber; $arrMember['address'] = $address; $arrMember['zipcode'] = $zipcode; $arrMember['userpic'] = $userpic; $arrMember['clothsize'] = $clothsize; //var_dump($arrMember); }else{ //-----显示队员信息 $strAction = $_SESSION['teamname']; $arrMember = $this->Team_model->getMemberInfo($inUserid); $data['strClothOpt'] = $this->Common_model->getClothOpy($arrMember['clothsize']); } $data['straction'] = "alert"; $data['arrMember'] = $arrMember; $data['strAction'] = $strAction; $this->load->view('head'); $this->load->view('player',$data); $this->load->view('foot'); }




回复讨论(解决方案)

按模块化设计原则:有一个入口,一个出口。一个模块完成一件事情
显然你的设计是不符合的

$inUserid = $_GET['id'];   

不用判???合法性??很危?啊。

if($_POST['action']=="add"){ 。。。。

 if($_POST['action']=="alert"){

你只是做了分支,??有按模?分function?理。

谢谢两前辈,这是以前的旧程序,总觉得哪不对劲。 有了两位的提醒,明了 多谢!