2011-05-28 18 views
14

Kohana'da bir form sihirbazı oluşturmaya çalışıyorum ve gittiğimde biraz öğreniyorum. Öğrendiğim şeylerden en iyi olanı, bir kullanıcı form oluşturma sürecinde olabilecek farklı adımları yönetmek için sınıf yapımda bir durum modeli kullanmaktır.Arabirimleri Kohana'da Kullanma 3.1.3

Bazı araştırmalar yaptıktan sonra, en iyi yaklaşımın bir arabirim kullanmak ve tüm adımları, arabirimi uygulayan durumlar olarak hareket etmek olabileceğini düşünüyordum. Bir durum onaylandıktan sonra, bir oturum değişkenini bir sonraki adıma değiştirir, bu arayüzün ilk yükü üzerine okunabilir ve kullanılacak doğru durumu arayabilir.

Bu yaklaşım mantıklı mı? Eğer öyleyse, halt nasıl yaptıklarını gerçekleştir (nasıl iyi yapı dosya sistemi mi?)

Burada üzerinde çalışıyoruz kaba bir başlangıçtır:

<?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(); 
+1

Temel olarak, [state pattern] 'i (http://en.wikipedia.org/wiki/State_pattern) çözdüğünüz gibi kullanıyorsunuz. Bu soru için hala yardıma ihtiyacınız var mı? – Ikke

+0

Farklı yaklaşımları duymakla kesinlikle ilgilenirim. –

cevap

1

Yolunuz kesinlikle mümkün görünüyor Bununla birlikte, onu daha basit tutmak ve istediğin şeylere dikkat etmek için bazı Kohanas yapılarını kullanmak için cazip gelebilirim. Örneğin, Kostache (bıyık) kullanırım ve her adım için ayrı View sınıfları (ve potansiyel olarak şablonlar) kullanırdım. Sonra kontrolör oldukça basit hale gelir. Aşağıdaki örneğe bakın (eksik oturum şeyleri ve step_number'in geçerliliği). Tüm doğrulama, modelde ele alınır. Bir doğrulama hatası varsa, daha sonra hata iletilerini View'a geri aktaran bir istisna atılabilir.

<?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(); 
    } 
} 
?> 

Bunun mantıklı olmasını umuyorum.

+0

Bu mantıklı ve ben bu satırlarda daha fazla düşünmekteyim, ancak denetleyicideki onaylamadan sonra elemanları eklediğinizde, denetleyicimi gerçekten şişiriyor. Hala bir şeyler yapmanın daha temiz bir yolu olmalı diye düşünüyorum ... –

İlgili konular