现在我正在尝试在控制器类中创建一些应该更新博客文章的函数。在控制器超类中,有一个函数可以通过 url 中的方法名和参数执行函数。当我将视图定向到编辑功能时,编辑功能可以正常工作。但是,当 set 方法将任务数组更改为更新时,它应该重定向 url 以执行更新功能。然而,它并没有这样做。我尝试在更新函数中添加参数,我尝试在更新函数中删除所有参数。我什至尝试编辑视图,以便它在任务数组设置为更新时再次提交。在声明任务集方法时,我究竟应该做什么来调用更新函数。如果我的措辞听起来令人困惑,我深表歉意,但它'
这是 Controller 超类的代码
<?php
class Controller {
public $load;
public $data = array();
function __construct($view, $method = null, $parameters = null){
//instantiate the load class
$this->load = new Load();
new Model();
//run any task methods
if($method){
$this->runTask($method, $parameters);
}else{
$this->defaultTask();
}
//render the view
$this->load->view($view.'.php', $this->data);
}
/*
*The runTask() method is our way of grabbing the method from the URI string and parsing the parameters
*/
public function runTask($method, $parameters = null){
if($method && method_exists($this, $method)) {
//the call_user_func_array expects an array so we create a null array if parameters is empty
if(!is_array($parameters)){
$parameters = array();
}
call_user_func_array(array($this, $method), $parameters);
}
}
/*
*The defaultTask() method is the one run if no task method is run. Here as a placeholder for child classes.
*/
public function defaultTask(){
}
/*
*The set() method allows us to more easily set the view variables
*/
public function set($key, $value){
$this->data[$key] = $value;
}
}
这是控制器的子类,其中编辑和更新功能
<?php
class AddPostController extends Controller{
public $postObject;
public function defaultTask(){
$this->postObject = new Post();
$this->set('task', 'add');
}
public function add(){
$this->postObject = new Post();
$data = array('title'=>$_POST['post_title'],'content'=>$_POST['post_content'],
'categoryID'=>$_POST['categoryID'],'date'=>$_POST['date']);
$result = $this->postObject->addPost($data);
$this->set('message', $result);
}
public function edit($pID){
$this->postObject = new Post();
$post = $this->postObject->getPost($pID);
$this->set('pID', $post['pID']);
$this->set('date', $post['date']);
$this->set('categoryID', $post['categoryID']);
$this->set('title', $post['title']);
$this->set('content', $post['content']);
$this->set('task', 'update'); //This should redirect to update method
}
public function update() {
echo 'controller update';
echo sizeof($data);
echo sizeof($this->data);
//$this->postObject->update($data);
}
}
这是添加或编辑帖子视图
<?php include('elements/admin_header.php');?>
<div class="container">
<div class="page-header">
<h1> the Add Post View </h1>
</div>
<?php if($message){?>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<?php echo $message?>
</div>
<?php }?>
<div class="row">
<div class="span8">
<form method="post" action="" name="edit">
<label>Edit Post #</label>
<select name="pID" onchange="edit.submit();">
<option value="Post">Post</option>
<?php
if(is_numeric($_POST['pID']) ==false){
}else{
echo '<option selected value='.$_POST['pID'].'>'.$_POST['pID'].'</option>';
$task='edit/'.$_POST['pID'];
}
/*if(is_numeric($_POST['pID'])==false){
$update=false;
}else{
echo '<option selected="selected" value='.$_POST['pID'].'>'.$_POST['pID'].'</option>';
$update=true;
}*/
//This is to populate post number select.
$post = new Post();
$maxPost=sizeof($post->getAllPosts());
for($postNum=1; $postNum<=$maxPost; $postNum++){
echo '<option value='.$postNum.'>'.$postNum.'</option>';
}
?>
</select>
</form>
<form action="<?php echo BASE_URL?>addpost/<?php echo $task?>" method="post" onsubmit="editor.post()">
<label>Title</label>
<input type="text" class="span6" name="post_title" value="<?php echo $title?>">
<label>Content</label>
<textarea id="tinyeditor" name="post_content" style="width:556px;height: 200px"><?php echo $content?></textarea>
<br/>
<input type="hidden" name="pID" value="<?php echo $pID?>"/>
<label>Date</label>
<input type="date" name="date" />
<label>Category ID</label>
<input type="number" name="categoryID" /> <br />
<button id="submit" type="submit" class="btn btn-primary" >Submit</button>
</form>
</div>
</div>
</div>
<?php include('elements/admin_footer.php');?>