我知道.NET和PHP,根据我的理解,.NET中的EntityFramework和PHP中的Laravel是一样的。
在.NET中,我们有@using (Ajax.BeginForm
,它在PHP中呈现与{!! Form::open
相同的表单。
@using (Ajax.BeginForm
不会刷新表单提交的整个页面,我也不需要编写JQuery来完成这项工作。因此,基本上它的作用与JQuery.ajax
相同
问题:有没有任何方法可以在不使用Laravel编写JQuery的情况下对表单提交进行部分页面刷新?
奖金问题我们还可以选择传递参数,如onSuccess、OnComplete、OnBegin和用于传递表单css类的param。我们能在拉拉维尔做到这一点吗。这在.NET 的实体框架中是可能的
简单地说:没有
但是,您可以创建自己的包来为您完成这项工作。
创建一个简单的类来打开和关闭表单:
namespace Ajaxform;
Class AjaxForm {
public function begin($action, $method, $options){
//do other stuff with options, minimal example
return '<form action="'.$action.'" method="'.$method.'" data-ajax-form/>';
}
public function end(){
return '</form>';
}
}
然后创建一个Facade访问器:
namespace AjaxForm'Facades;
Class AjaxFormFacade extends Illuminate'Support'Facades'Facade{
public static function getFacadeAccessor(){
return 'ajaxform';
}
}
然后创建一个服务提供商,将我们的facade绑定到应用程序:
namespace AjaxForm'Providers;
public function AjaxFormServiceProvider {
public function bind(){
$this->publishes([
__DIR__.'/../assets/' => public_path('vendor/ajaxform')
], 'public');
}
public function register(){
$this->app->bind('ajaxform', function(){
return new AjaxForm;
}
}
}
你的供应商包看起来像这样:
MyVendor (replace this with whatever you want)
- Src
- AjaxForm
- assets
ajax-form.js
- Facades
AjaxFormFacade.php
- Providers
AjaxFormServiceProvider.php
AjaxForm.php
然后,您的composer.json
在AjaxForm
(父目录,位于Src目录之前)中看起来是这样的
{
"name": "myvendor/ajax-form",
"version" : "master",
"description": "An Ajax Form Wrapper.",
"license" : "MIT",
"keywords": ["ajaxform"],
"authors" : [
{
"name": "My Name",
"email": "wheredoigetahold@of.you"
}
],
"require": {
"php": ">=5.5",
"illuminate/support": "4.*|5.*"
},
"autoload": {
"psr-4": {
"Ajaxform''": "src/AjaxForm/"
}
},
"minimum-stability": "stable"
}
现在您已经创建了供应商包,将其添加到config/app.php
文件中,并向IOC介绍我们的新外观和提供商:
找到providers
数组并在末尾添加:
MyVendor'AjaxForm'AjaxForm::class
然后在Facades数组中,在末尾添加以下内容:
'AjaxForm' => 'MyVendor'AjaxForm'AjaxForm::class
现在,您只需要创建ajax-form.js
文件,它将具有我们对表单进行调整的逻辑。
jQuery(function($){
$('form[data-ajax-form]').on('submit', function(e){
e.preventDefault();
var $this = $(this);
$.ajax({
method: $this.prop('method'),
url: $this.prop('action'),
data: $this.serialize()
}).done(function(resp){
//put some logic here
}).error(function(err){
//put some logic here
});
});
});
现在您可以调用您的表单:
{{AjaxForm::begin(route('path.to.my.endpoint'), 'POST') }}
//form inputs go in here
{{AjaxForm::end()}}