laravel form post issue

2019-02-13 22:19发布

I am building a practice app with the Laravel framework I built a form in one of the views which is set to post to 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.

Here is my view 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

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

7条回答
家丑人穷心不美
2楼-- · 2019-02-13 22:37

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

@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

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');          
});
查看更多
Viruses.
3楼-- · 2019-02-13 22:38

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.

查看更多
家丑人穷心不美
4楼-- · 2019-02-13 22:38

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

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

查看更多
祖国的老花朵
5楼-- · 2019-02-13 22:41
{{ Form::open(array('url' => ' ', 'method' => 'post')) }}

Passing a space as the url has worked for me.

查看更多
手持菜刀,她持情操
6楼-- · 2019-02-13 22:45

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

{{ 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:

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

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

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

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

BYE

查看更多
SAY GOODBYE
7楼-- · 2019-02-13 23:00

I think this post: Form submits as GET Laravel 4 is 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.

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.

查看更多
登录 后发表回答