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

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
Ragzor
  • 160
  • 1
  • 2
  • 12
  • Route::any('/', function() { return View::make('index'); }); – Ahmed Abumostafa Jul 07 '13 at 11:20
  • what exactly are you trying to do?? – Trying Tobemyself Jul 07 '13 at 14:54
  • Can you show me the result when you use the `index` view file in your `POST` route? – Hieu Le Jul 07 '13 at 15:02
  • 1) What URL are you using to access the form? 2) What *other* routes do you have defined? – fideloper Jul 07 '13 at 16:04
  • @TryingTobemyselfRahul what I want to do is that when someone submits the form it gets posted to the same view itself and I see the 'success' message. Thanks for your reply! – Ragzor Jul 07 '13 at 16:12
  • @Trung-HieuLe I see the exact same view and nothing changes its just like the page refreshes. I am supposed to see the successfull message right. Thanks for your reply! – Ragzor Jul 07 '13 at 16:13
  • @fideloper I am using the home url in my case it is shorten.dev/ which points to localhost:8888/shorten/public/ basically it is the home route and I have no other Routes Defined. this is the whole content of the routes.php file. Thanks for your reply! – Ragzor Jul 07 '13 at 16:14
  • Is this the exact code are you trying with? Because its working for me, can you show your code? – Trying Tobemyself Jul 07 '13 at 18:00
  • 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/`. – fideloper Jul 07 '13 at 18:32
  • hello @TryingTobemyselfRahul yes it is exactly the same code – Ragzor Jul 07 '13 at 19:28
  • @fideloper yes I tried that as well :/ i removed the alias shortened.dev/ and went back to the regular localhost localhost:8888 and I also tried removing and adding the slash '/' but chrome seems to automatically inserting the slash. – Ragzor Jul 07 '13 at 19:30
  • did you try in another browser? – Trying Tobemyself Jul 07 '13 at 19:35

7 Answers7

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

Passing a space as the url has worked for me.

Raul
  • 126
  • 1
  • 3
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.

Community
  • 1
  • 1
user1262878
  • 67
  • 2
  • 9
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.

tylerjwilk
  • 11
  • 1
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

Domenico Monaco
  • 1,236
  • 12
  • 21
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.

Markus Hofmann
  • 3,427
  • 4
  • 21
  • 31
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');          
});
Hieu Le
  • 8,288
  • 1
  • 34
  • 55
  • 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 :/ – Ragzor Jul 07 '13 at 17:12
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']);
llioor
  • 5,804
  • 4
  • 36
  • 44