我正在尝试在Kohana中构建一个表单向导,并且我正在学习一点。我学到的最好的事情之一就是在我的类结构中使用状态模式来管理用户在表单处理过程中可以处于的不同步骤。
在做了一些研究之后,我一直在想最好的方法可能是使用接口并让所有步骤都充当实现接口的状态。在状态验证之后,它会将会话变量更改为下一步,可以在初始加载接口时读取并调用正确的状态以供使用。
这种方法有意义吗?如果是这样,我该如何实现(如何最好地构建文件系统?)
这是我一直在努力的艰难开始:
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Project_Builder @state
* Step_One @state
* Step_Two @state
**/
interface Project_Builder
{
public function do_this_first();
public function validate();
public function do_this_after();
}
class Step_One implements Project_Builder {
public function __construct
{
parent::__construct();
// Do validation and set a partial variable if valid
}
public function do_this_first()
{
echo 'First thing done';
// This should be used to set the session step variable, validate and add project data, and return the new view body.
$session->set('step', '2');
}
public function do_this_after()
{
throw new LogicException('Have to do the other thing first!');
}
}
class Step_Two implements Project_Builder {
public function do_this_first()
{
throw new LogicException('Already did this first!');
}
public function do_this_after()
{
echo 'Did this after the first!';
return $this;
}
}
class Project implements Project_Builder {
protected $state;
protected $user_step;
protected $project_data
public function __construct()
{
// Check the SESSION for a "step" entry. If it does not find one, it creates it, and sets it to "1".
$session = Session::instance('database');
if ( ! $session->get('step'))
{
$session->set('step', '1');
}
// Get the step that was requested by the client.
$this->user_step = $this->request->param('param1');
// Validate that the step is authorized by the session.
if ($session->get('step') !== $this->user_step)
{
throw new HTTP_Exception_404('You cannot skip a step!');
}
// Check if there is user data posted, and if so, clean it.
if (HTTP_Request::POST == $this->request->method())
{
foreach ($this->request->post() as $name => $value)
{
$this->project_data["$name"] = HTML::chars($value);
}
}
// Trigger the proper state to use based on the authorized session step (should I do this?)
$this->state = new Step_One;
}
public function doThisFirst()
{
$this->state = $this->state->do_this_first();
}
public function doThisAfter()
{
$this->state = $this->state->do_this_after();
}
}
$project = new Project;
try
{
$project->do_this_after(); //throws exception
}
catch(LogicException $e)
{
echo $e->getMessage();
}
$project = new Project;
$project->do_this_first();
$project->validate();
$project->do_this_after();
//$project->update();
答案 0 :(得分:1)
你的方式当然看起来可能,但是我会想要保持简单,并使用一些Kohanas内置功能来照顾你想要的东西。例如,我会使用Kostache(小胡子)并为每个步骤分别使用View类(和可能的模板)。然后控制器变得非常简单。请参阅下面的示例(缺少会话内容和step_number的验证)。所有验证都在模型中处理。如果存在验证错误,则可以抛出异常,然后将错误消息传递回View。
<?php
class Wizard_Controller {
function action_step($step_number = 1)
{
$view = new View_Step('step_' + $step_number);
if ($_POST)
{
try
{
$model = new Model_Steps;
$model->step_number = $step_number;
if ($model->save($_POST))
{
// Go to the next step
$step_number++;
Request::current()->redirect('wizard/step/'.$step_number);
}
}
catch (Some_Kind_Of_Exception $e)
{
$view->post = $_POST;
$view->errors = $e->errors();
}
}
$view->render();
}
}
?>
希望这是有道理的。