5

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

12
  • Route::any('/', function() { return View::make('index'); }); Commented Jul 7, 2013 at 11:20
  • what exactly are you trying to do?? Commented Jul 7, 2013 at 14:54
  • Can you show me the result when you use the index view file in your POST route? Commented Jul 7, 2013 at 15:02
  • 1) What URL are you using to access the form? 2) What other routes do you have defined? Commented Jul 7, 2013 at 16:04
  • 1
    @Ragzor - great, so you are able to view the site at shorten.dev/? I think you see my point - your form being set to submit to /, which should be going to shorten.dev/ and not to localhost:8888/ since your code is not "listening" at localhost:8888/. Commented Jul 7, 2013 at 18:32

7 Answers 7

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

Passing a space as the url has worked for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Route::post('/', 'UserController@login'); doesn't work unless the form url is empty, this is true only for post to homepage
worked for me too, but does anyone know why this weird issue?
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.

Comments

1

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.

Comments

1

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

Comments

0

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.

Comments

0

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

1 Comment

Hello!Thanks alot for the reply, Yes this is the correct way of doing it However this is also not working in this case I found out that when the app is making a post request I get a 301 moved permanently header so it just gets back to the get request and I see the default view. I tried the same thing on laravel 3 and it was working. No clue what is going on here :/
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.