Php 如何在Laravel中从XML构建表单
更新:Php 如何在Laravel中从XML构建表单,php,laravel-4,Php,Laravel 4,更新: 我知道如何解析XML,但不知道它在体系结构中的具体位置,请参阅下面定义的问题。 欢迎所有建议 我努力进入Laravel,尝试从XML文件构建表单 问题: 如何将数据从XML获取到视图中 在何处构建表单-在重复方面,我更愿意创建一次表单,并使用它进行创建、编辑和查看 验证-我希望尽可能地重用它 XML:foods\u form.XML(简化): Laravel不会帮助您从一些XML创建表单 您需要使用类似于SimpleXML的库解析XML:您将找到一些文档 从创建SimpleXMLEle
我知道如何解析XML,但不知道它在体系结构中的具体位置,请参阅下面定义的问题。
欢迎所有建议
我努力进入Laravel,尝试从XML文件构建表单 问题:
Laravel不会帮助您从一些XML创建表单 您需要使用类似于
SimpleXML
的库解析XML:您将找到一些文档
从创建SimpleXMLElement开始:
$xml = new SimpleXMLElement('../path/to/your/file/foods_form.xml', 0, true);
现在,您可以使用$xml
对象生成表单,这与xml的格式有关(转储$xml
对象以了解其结构)
只需将对象放在视图中,即可直接使用它
要验证表单,可以使用Laravel的
验证:我还没有找到一个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个额外的依赖项来包含我要执行的任何逻辑。并使用最简单的类来应用我的xsl
<?php
class SomeController extends SomeMvcController {
private $someModuleLogic;
private $otherModuleLogic;
private $lastModuleLogic;
private $xslTransformer;
public function __construct( XslTransformer $xslTransformer, $someModuleLogic, $otherModuleLogic, $lastModuleLogic ) {
$this->someModuleLogic = $someModuleLogic;
$this->otherModuleLogic = $otherModuleLogic;
$this->lastModuleLogic = $lastModuleLogic;
parent::__construct();
$this->xslTransformer = $xslTransformer;
}
public function someAction() {
/**
* doStuff functions will take your parameters like get, post etc and return a DomDocument object
* which can be programmatically calculated via PHP or generated by reading an XML file (or xml from any buffer)
*/
$someModule = $this->xslTransformer->transform(
'myViews/modules/someModule.xsl',
$this->someModeuleLogic->doStuff()
);
$otherModule = $this->xslTransformer->transform(
'myViews/modules/otherModule.xsl',
$this->otherModeuleLogic->doStuff()
);
$lastModule = $this->xslTransformer->transform(
'myViews/modules/lastModule.xsl',
$this->lastModeuleLogic->doStuff()
); }
}
class XslTransformer {
public function transform( $xslLocation, DOMDocument $domDocument ) {
$xslDocument = new DOMDocument();
$xslDocument->load( $xslLocation );
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet( $xslDocument );
$document = $xsltProcessor->transformToDoc( $domDocument );
$document->encoding = 'UTF-8';
$document->formatOutput = true;
$document->preserveWhiteSpace = false;
return $document;
}
}
xslTransformer->transform(
“myViews/modules/someModule.xsl”,
$this->someModeuleLogic->doStuff()
);
$otherModule=$this->xsltranformer->transform(
“myViews/modules/otherModule.xsl”,
$this->otherModeuleLogic->doStuff()
);
$lastModule=$this->xsltranformer->transform(
“myViews/modules/lastModule.xsl”,
$this->lastModeuleLogic->doStuff()
); }
}
类XslTransformer{
公共函数转换($xslLocation,DOMDocument$DOMDocument){
$xslDocument=newdomdocument();
$xslDocument->load($xslLocation);
$xsltProcessor=新的xsltProcessor();
$xsltProcessor->importStylesheet($xslDocument);
$document=$xsltProcessor->transformToDoc($domDocument);
$document->encoding='UTF-8';
$document->formatOutput=true;
$document->preserveWhiteSpace=false;
返回$document;
}
}
这使得我的视图/控制器非常小且简单,没有任何逻辑。每件事都是在课堂上完成的,我可以把它切成简单的小块 下面是一个开始,您可以构建一个类来处理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');
然后你的观点会是这样的
@extends('layout')
@section('content')
<form action="{{ action('FormsController@handleCreate') }}" method="post" role="form">
{{ $form_content }}
<input type="submit" value="Create" />
<a href="{{ action('FormsController@index') }}">Cancel</a>
</form>
@stop
@extends('layout'))
@节(“内容”)
{{$form_content}}
@停止
好的,我可以解析文件。对我来说,将这种逻辑编码到视图中似乎不太像MVC。有什么想法吗?创建一个类,将$xml
解析为HTML表单,并通过构造函数将其注入控制器?然后您的控制器可以执行如下操作:$xml=newsimplexmlement('../path/to/forms.xml')$html=$this->xml\u form\u parser->parseToHtml($xml)代码>谢谢,这非常有帮助!谢谢,我是XSL新手,一定会研究它的。
<?php
class SomeController extends SomeMvcController {
private $someModuleLogic;
private $otherModuleLogic;
private $lastModuleLogic;
private $xslTransformer;
public function __construct( XslTransformer $xslTransformer, $someModuleLogic, $otherModuleLogic, $lastModuleLogic ) {
$this->someModuleLogic = $someModuleLogic;
$this->otherModuleLogic = $otherModuleLogic;
$this->lastModuleLogic = $lastModuleLogic;
parent::__construct();
$this->xslTransformer = $xslTransformer;
}
public function someAction() {
/**
* doStuff functions will take your parameters like get, post etc and return a DomDocument object
* which can be programmatically calculated via PHP or generated by reading an XML file (or xml from any buffer)
*/
$someModule = $this->xslTransformer->transform(
'myViews/modules/someModule.xsl',
$this->someModeuleLogic->doStuff()
);
$otherModule = $this->xslTransformer->transform(
'myViews/modules/otherModule.xsl',
$this->otherModeuleLogic->doStuff()
);
$lastModule = $this->xslTransformer->transform(
'myViews/modules/lastModule.xsl',
$this->lastModeuleLogic->doStuff()
); }
}
class XslTransformer {
public function transform( $xslLocation, DOMDocument $domDocument ) {
$xslDocument = new DOMDocument();
$xslDocument->load( $xslLocation );
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet( $xslDocument );
$document = $xsltProcessor->transformToDoc( $domDocument );
$document->encoding = 'UTF-8';
$document->formatOutput = true;
$document->preserveWhiteSpace = false;
return $document;
}
}
<?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');
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('FormsController@handleCreate') }}" method="post" role="form">
{{ $form_content }}
<input type="submit" value="Create" />
<a href="{{ action('FormsController@index') }}">Cancel</a>
</form>
@stop