Reputation: 160
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?
I see that when the form is making the post request I am getting a 301 MOVED PERMANENTLY HEADER
Upvotes: 5
Views: 11558
Reputation: 6238
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']);
Upvotes: 0
Reputation: 1236
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
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 126
{{ Form::open(array('url' => ' ', 'method' => 'post')) }}
Passing a space as the url has worked for me.
Upvotes: 11
Reputation: 67
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.
Upvotes: 2
Reputation: 8415
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');
});
Upvotes: 0
Reputation: 3447
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.
Upvotes: 0