2014-01-21 22 views
5

更新:
我知道如何解析XML,但不正是在架构,见下面定义的问题。
欢迎您提出任何建议!如何从XML在Laravel建立一个形式


为了进入Laravel,我尝试从XML文件构建表单。

问题:

  1. 如何从XML到视图中的数据?
  2. 其中构建形式 - 在重复而言,我宁愿一次创建表单,并使用它来创建,编辑和查看
  3. 验证 - 我想重新使用它不亚于可能

的XML: foods_form.xml(简化):

<form> 
    <field id="1" name="cheese" label="favorite cheese?" type="radio" req="1" filter="int"> 
     <option id="1" value="1">Camembert</option> 
     <option id="2" value="3">Gouda</option> 
    </field> 
    <field id="2" name="beer" label="favorite beer?" type="text" req="1" filter="str" /> 
</form> 

视图:应用/视图/ create.blade.php:

@extends('layout') 
@section('content') 

<form action="{{ action('[email protected]') }}" method="post" role="form"> 

    @foreach ($fields as $field) 
     <label for="{{ $field->name }}">{{ $field->label }}</label> 

     @if ($field->type == 'text') 
      <input type="text" name="{{ $field->name }}" /> 
     @else 
      @foreach ($field->option as $option) 
       <input type="radio" name="{{ $field->name }}" value="{{ $option }}" /> 
      @endforeach 
     @endif 
    @endforeach  

    <input type="submit" value="Create" /> 
    <a href="{{ action('[email protected]') }}">Cancel</a> 
</form> 
@stop 

控制器:应用程序/控制器/ FormsController.php:

class TestsController extends BaseController { 

    public function index() { 
     // return some view 
    } 

    public function create() { 
     return View::make('create'); 
    } 

    public function handleCreate() { 
     // validation according to XML 
     // save to database if valid || return to form if not valid 
    } 
} 

回答

-1

下面是您可以构建的用于处理XML到HTML表单转换的类的类型的开始。

<?php 

class XmlToHtmlFormConverter { 


    public function buildFormContent($filename) 
    { 
     $xml_fields = new SimpleXmlElement($filename, 0, true); 
     $html = ''; 

     foreach ($xml_fields as $field) { 
      $attributes = $field->attributes(); 
      $html .= '<label for="'.$attributes['name'].'">'.$attributes['label'].'</label>'.PHP_EOL; 

      if ('text' == $attributes['type']) { 
       $html .= '<input type="text" name="'.$attributes['name'].'" />'.PHP_EOL; 
      } else { 
       $html .= $this->buildOptionInputs($field); 
      } 
     } 

     return $html; 
    } 


    protected function buildOptionInputs($field) 
    { 
     $html = ''; 
     $attributes = $field->attributes(); 
     foreach ($field->option as $option) { 
      $html .= '<input type="radio" name="'.$attributes['name'].'" value="'.$option.'" />'.PHP_EOL; 
     } 
     return $html; 
    } 
} 

// Uncomment below to actually see the output, this works with your xml file. 
// $converter = new XmlToHtmlFormConverter; 
// echo $converter->buildFormContent('form.xml'); 

正如前面的回答所述,您可以将此类注入到您的控制器构造函数中。 如果你想变得有趣,你可以创建一个接口并注入接口,然后用App :: bind('SomeInterface','SomeImplementation')将你的实现绑定到该接口。但要保持简单,您可以直接注入课程。

控制器:

class TestsController extends BaseController { 

    protected $xml_to_html_form_converter; 


    public function __construct(XmlToHtmlFormConverter $xml_to_html_form_converter) 
    { 
     $this->xml_to_html_form_converter = $xml_to_html_form_converter; 
    } 


    public function index() { 
     // return some view 
    } 


    public function create() { 
     $xml_file_path = 'some/path/xmlfile.xml'; 
     return View::make('create')->with(array(
      'form_content' => $this->xml_to_html_form_converter->buildFormContent($xml_file_path); 
     )); 
    } 


    public function handleCreate() { 
     // do your validations like you would with any html form 
    } 
} 

然后您认为会是这样......

@extends('layout') 
@section('content') 

<form action="{{ action('[email protected]') }}" method="post" role="form"> 

    {{ $form_content }} 

    <input type="submit" value="Create" /> 
    <a href="{{ action('[email protected]') }}">Cancel</a> 
</form> 
@stop 
+0

谢谢,这非常有帮助! – michi

5

Laravel不会帮助您创建一些XML表单。

你需要与像SimpleXML库来解析您的XML:你会发现一些文件here, on php.net

开始创建一个SimpleXMLElement:

$xml = new SimpleXMLElement('../path/to/your/file/foods_form.xml', 0, true); 

您现在可以使用您的$xml对象生成您的表格,尊重您的XML格式(转储您的$xml对象以了解结构)

只需将您的对象放在您的视图中即可直接使用它年。

要验证表单,您可以使用Validation从Laravel:link to the doc

+0

OK,我可以分析该文件。将该逻辑编码到视图中看起来不像MVC那样。有任何想法吗? – michi

+0

创建一个类来将'$ xml'解析为一个HTML表单并通过构造函数将它注入到控制器中?然后你的控制器可以做这样的事情:'$ xml = new SimpleXMLElement('../ path/to/forms.xml'); $ html = $ this-> xml_form_parser-> parseToHtml($ xml);' – patricksayshi

1

我还没有找到一个XSL友好MVC。

我这样做(在视图XSL的巨大的风扇)的方式

最后一页都会有3个模块让说:

我的观点仅包含

// I don't know how you simply echo a variable from "yet another framework" 
// but this is the only thing you need to do from the view 
echo $someModule; 
echo $otherModule; 
echo $lastModule; 

我的控制器将有3个额外的依赖关系injected包含我得到执行的任何逻辑。并使用最简单的类来应用我的xsl这使我的视图/控制器非常小而且没有任何逻辑的简单性。一切都是在注入课程中完成的,我可以将其切成小块简单的片段。

+0

谢谢,我是XSL新手,一定会考虑它。 – michi