在Kohana 3.1.3中使用接口

Dav*_*iss 14 php oop interface kohana

我正在尝试在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();
Run Code Online (Sandbox Code Playgroud)

ae.*_*ae. 1

你的方式看起来当然是可行的,但是我很想保持它更简单,并使用 Kohanas 的一些内置功能来满足你的需求。例如,我将使用 Kostache(小胡子),并为每个步骤设置单独的 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();
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

希望这是有道理的。