laravel form post issue

跟風遠走 提交于 2019-12-30 06:54:30

问题


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


回答1:


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

Passing a space as the url has worked for me.




回答2:


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.




回答3:


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:


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




回答5:


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

  • Resource Controllers (or RESTful Controllers)
  • Forms & HTML
    • Opening A Form
  • Routing
    • Named Routes

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




回答6:


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');          
});



回答7:


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

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

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']);


来源:https://stackoverflow.com/questions/17511653/laravel-form-post-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!