2011-05-28 13 views
14

मैं कोहाना में एक फॉर्म विज़ार्ड बनाने की कोशिश कर रहा हूं और जैसे ही मैं जाता हूं, सीख रहा हूं। जो चीजें मैंने सीखी हैं उनमें से एक सबसे अच्छा काम कर सकती है, मेरी कक्षा संरचना में एक राज्य पैटर्न का उपयोग करना है ताकि उपयोगकर्ता प्रक्रिया के दौरान उपयोगकर्ता के विभिन्न चरणों का प्रबंधन कर सके।कोहाना में इंटरफेस का उपयोग 3.1.3

कुछ शोध करने के बाद, मैं सोच रहा हूं कि इंटरफ़ेस का उपयोग करने का सबसे अच्छा तरीका हो सकता है और सभी चरण इंटरफ़ेस को लागू करने वाले राज्यों के रूप में कार्य करते हैं। एक राज्य मान्य होने के बाद, यह एक सत्र चर को अगले चरण में बदल देगा, जिसे इंटरफ़ेस के प्रारंभिक लोड पर पढ़ा जा सकता है और उपयोग करने के लिए सही स्थिति को कॉल कर सकता है।

क्या यह दृष्टिकोण समझ में आता है? यदि हां, तो कैसे हो कर मैं ऐसा कर (मैं कैसे सबसे अच्छा संरचना फाइल सिस्टम करते हैं?)

यहाँ किसी न किसी तरह शुरू मैं पर काम कर रहा हूँ है:

<?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

आप मूल रूप से [राज्य पैटर्न] (http://en.wikipedia.org/wiki/State_pattern) का उपयोग कर रहे हैं जैसा कि आपने पाया होगा। क्या आपको अभी भी इस प्रश्न के साथ मदद की ज़रूरत है? – Ikke

+0

मैं निश्चित रूप से विभिन्न दृष्टिकोण सुनने में रुचि रखूंगा। –

उत्तर

1

आपका तरीका निश्चित रूप से संभव लग रहा है, हालांकि मैं इसे सरल रखने और कोहना के कुछ उपयोगों का निर्माण करने के लिए प्रेरित हूं जो कि आप जो चाहते हैं उसकी देखभाल करने के लिए। उदाहरण के लिए, मैं कोस्टैच (मूंछ) का उपयोग करता हूं और प्रत्येक चरण के लिए अलग दृश्य वर्ग (और संभावित टेम्पलेट्स) का उपयोग करता हूं। फिर नियंत्रक काफी सरल हो जाता है। नीचे दिया गया उदाहरण देखें (अनुपलब्ध सत्र सामग्री और step_number की सत्यापन)। मॉडल में सभी सत्यापन को संभाला जाता है। यदि कोई सत्यापन त्रुटि है, तो एक अपवाद फेंक दिया जा सकता है जो फिर त्रुटि संदेशों को दृश्य में वापस भेज सकता है।

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

आशा है कि यह समझ में आता है।

+0

यह समझ में आता है, और मैं इन पंक्तियों को और अधिक सोच रहा हूं, लेकिन मेरे नियंत्रक में सत्यापन के बाद सत्र में तत्वों को जोड़कर वास्तव में मेरे नियंत्रक को फूला हुआ है। अभी भी सोच रहा है कि चीजों को करने का एक साफ तरीका होना चाहिए ... –

संबंधित मुद्दे