Php 为什么赢了';我的控制器中的编辑方法是否将url重定向到更新方法?
现在我正在尝试在控制器类中创建一些函数,这些函数应该用来更新博客文章。在控制器超类中,有一个函数使其能够通过url中的方法名和参数执行函数。当我将视图指向编辑功能时,编辑功能工作正常。但是,当set方法将任务数组更改为update时,它应该重定向url以执行update函数。然而,它并没有这样做。我尝试在更新函数中添加参数,我尝试删除更新函数中的所有参数。我甚至尝试过编辑视图,以便在任务数组设置为更新时再次提交视图。在声明任务集方法时,我应该做什么来调用update函数呢。如果我的措辞听起来令人困惑,我很抱歉,但很难解释这个问题 下面是控制器超类的代码Php 为什么赢了';我的控制器中的编辑方法是否将url重定向到更新方法?,php,model-view-controller,view,controller,Php,Model View Controller,View,Controller,现在我正在尝试在控制器类中创建一些函数,这些函数应该用来更新博客文章。在控制器超类中,有一个函数使其能够通过url中的方法名和参数执行函数。当我将视图指向编辑功能时,编辑功能工作正常。但是,当set方法将任务数组更改为update时,它应该重定向url以执行update函数。然而,它并没有这样做。我尝试在更新函数中添加参数,我尝试删除更新函数中的所有参数。我甚至尝试过编辑视图,以便在任务数组设置为更新时再次提交视图。在声明任务集方法时,我应该做什么来调用update函数呢。如果我的措辞听起来令人
<?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;
}
}
我提供了一些代码,这些代码可能会引导您走向正确的方向
public function set($key, $value){
// this following line only updates data and that's it
$this->data[$key] = $value;
// you need to call run task method, so that the new task will be executed
if($key == "task") {
$this->runTask($value, $parameters);
}
}
您可能需要更改set方法的签名以传递参数等,但这取决于您如何处理它
//编辑:如果键是“task”,那么通常执行runTask方法(从我在不同框架上看到的情况来看),这段代码不应该在基本控制器中
通常有一个router类接受URI并定义(根据一些命名约定或正则表达式路径列表)应该调用什么控制器和什么操作
然后它调用另一个类,通常称为“Dispatch”,该类创建控制器实例并调用传递所需参数的action方法
这可能是一种更干净的方法。。它将帮助您进行调试
希望这对您有所帮助当您使用set方法将任务设置为“更新”时,set方法仅将信息存储在数组中,而不执行任何其他操作。控制器如何知道任务已更改并执行它。您可能需要调用runTask来处理它,或者我遗漏了什么?设置任务应该将视图中的表单操作链接更改为更新,然后重定向它。至少我认为它应该是这样工作的我已经添加了一个额外解释的答案
<?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');?>
public function set($key, $value){
// this following line only updates data and that's it
$this->data[$key] = $value;
// you need to call run task method, so that the new task will be executed
if($key == "task") {
$this->runTask($value, $parameters);
}
}