Laravel 表单发布问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17511653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:06:00  来源:igfitidea点击:

laravel form post issue

phplaravellaravel-4

提问by Ragzor

I am building a practice app with the Laravelframework I built a form in one of the views which is set to postto the same view itself but when I hit submit the form is posted however I do not get the desired output, I see the original view again.

我正在使用Laravel框架构建一个练习应用程序我在其中一个视图中构建了一个表单,该表单被设置为发布到同一个视图本身但是当我点击提交时,表单被发布但是我没有得到所需的输出,我看到了原来的再次查看。

Here is my view index.blade.php

这是我的观点index.blade.php

@extends('master')

@section('container')

<div class="wrapper">

    {{ Form::open(array('url' => '/', 'method' => 'post')) }}
        {{ Form::text('url') }}
        {{ Form::text('valid') }}
        {{ Form::submit('shorten') }}
    {{ Form::close() }}

</div><!-- /wrapper -->

@stop 

and my routes.php

和我的routes.php

Route::get('/', function()
{
return View::make('index'); 
});

Route::post('/', function() 
{
return 'successfull';
});

What I've tried so far

到目前为止我尝试过的

  • I tried changing the post to a different view and it worked. However I want the form to post to the same view itself.

  • Instead of returning a string I tried to return make a view still it didn't work.

  • 我尝试将帖子更改为不同的视图并且它起作用了。但是我希望表单发布到同一个视图本身。

  • 我没有返回一个字符串,而是试图返回一个视图,但它仍然不起作用。

What am I doing wrong?

我究竟做错了什么?

addendum

附录

I see that when the form is making the post request I am getting a 301 MOVED PERMANENTLY HEADER

我看到当表单发出帖子请求时,我收到了 301 MOVED PERMANENTLY HEADER

回答by Raul

{{ Form::open(array('url' => ' ', 'method' => 'post')) }}

Passing a space as the url has worked for me.

传递一个空格作为 url 对我有用。

回答by user1262878

I think this post: Form submits as GET Laravel 4is related to your problem. I think the problem as I undersood it is caused by end a form url with a / . I found this when having problems to using post to a ./ url in my form. There is also a bug at github that seems like it is related https://github.com/laravel/framework/issues/1804.

我认为这篇文章:Form submits as GET Laravel 4与您的问题有关。我认为我理解的问题是由以 / 结尾的表单 url 引起的。我在表单中使用 post 到 ./ url 时遇到问题时发现了这一点。github 上还有一个错误,似乎与https://github.com/laravel/framework/issues/1804相关。

I know this is an old question but I found this thread having the same problem so hopefully someone else is helped by my answer.

我知道这是一个老问题,但我发现这个帖子有同样的问题,所以希望我的回答能帮助其他人。

回答by tylerjwilk

You need to make sure that your form's method does NOT end in a /for it to be routed correctly. For example if you have the following route:

您需要确保表单的方法不以 / 结尾才能正确路由。例如,如果您有以下路线:

Route::post('form/process', function()
{
   # code here ...    
});

Then you need to have the following form definition:

那么你需要有以下表单定义:

<form action="/form/process" method="POST">

I hope that helps.

我希望这有帮助。

回答by Domenico Monaco

I have same problem with OSx + MAMP, initially I've resolved with Raul's solution:

我对 OSx + MAMP 有同样的问题,最初我已经用 Raul 的解决方案解决了:

{{ Form::open(array('url' => ' ', 'method' => 'post')) }}

but after consultation with my friend we have concluded that my problem was due to the fact my lavarel project is avaliable by long local path, as:

但是在与我的朋友协商后,我们得出的结论是,我的问题是由于我的 lavarel 项目可以通过很长的本地路径获得,如下所示:

 http://localhost/custom/custom2/...

in this location the post/get method on root path ("/") not working correctly.

在此位置,根路径(“/”)上的 post/get 方法无法正常工作。

Lavarel to working correctly must be avaliable by "vhost", in this case the problem get/post method on root location "/" not exist.

Lavarel 必须通过“vhost”才能正常工作,在这种情况下,根位置“/”上的问题 get/post 方法不存在。

My friend advised me to use http://www.vagrantup.com/

我的朋友建议我使用http://www.vagrantup.com/

BYE

再见

回答by llioor

The problem is with defualt slashed in Apache from 2.0.51 and heigher: http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash

问题在于从 2.0.51 和更高版本的 Apache 中删除了默认值:http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash

The best solution if you do not want to change Apache config is to make the post in a different path:

如果您不想更改 Apache 配置,最好的解决方案是在不同的路径中发布帖子:

GOOD:

好的:

Route::get('/', 
  ['as' => 'wizard', 'uses' => 'WizardController@create']);

Route::post('wizard-post', 
  ['as' => 'wizard_store', 'uses' => 'WizardController@store']);

NOT GOOD:

不好:

Route::get('/', 
  ['as' => 'wizard', 'uses' => 'WizardController@create']);

Route::post('/', 
  ['as' => 'wizard_store', 'uses' => 'WizardController@store']);

回答by Markus Hofmann

There is some helpfull information in the Laravel Docs. Check these out:

Laravel 文档中有一些有用的信息。检查这些:

I recommend you read the Resource Controllersdocumentation as it makes form handling a lot easier.

我建议您阅读Resource Controllers文档,因为它使表单处理更容易。

回答by Hieu Le

Well, you just return the view, so nothing change. You should bind your route to a controller to do some logic and add data to your view, like this:

好吧,你只是返回视图,所以没有任何改变。您应该将您的路由绑定到控制器以执行一些逻辑并将数据添加到您的视图中,如下所示:

index.blade.php

index.blade.php

@extends('master')

@section('container')

<div class="wrapper">
     @if (isset($message))
     <p>{{$message}}</p>
     @endif

    {{ Form::open(array('url' => '/', 'method' => 'post')) }}
        {{ Form::text('url') }}
        {{ Form::text('valid') }}
        {{ Form::submit('shorten') }}
    {{ Form::close() }}

</div><!-- /wrapper -->

@stop

Your routes

您的路线

Routes::any('/', 'home@index');

You controller HomeController.php

你的控制器 HomeController.php

public function index()
{
     $data = array();
     $url = Input::get('url');
     if ($url)
          $data['message'] = "foo";

     return View::make('index', $data);
}

You can also modify your current routes without using a controller like this (use the new view file)

您还可以在不使用这样的控制器的情况下修改当前路由(使用新的视图文件)

Route::get('/', function()
{
     return View::make('index'); 
});

Route::post('/', function() 
{
     return View::make('index')->with('message', 'Foo');          
});