Question

So, i started with laravel. Tried with making a form post to the same page. Here's what i have so far,

routes.php

Route::get('/', 'HomeController@showWelcome');

Route::group(array('before' => 'csrf'), function () {
   Route::post('contactus', 'HomeController@sendEmail');
});

hello.php

<?php echo Form::open(array('action' => 'HomeController@sendEmail'))?>
input fields here
<?php echo Form::close() ?>

HomeController

public function showWelcome()
{
    return View::make('hello');
}

public function sendEmail()
{
    print_r($_POST);exit;
}

Problem: Form gets posted to the url public/contactus

Can someone point out which really stupid thing, i am doing?

Was it helpful?

Solution

routes.php

Route::get('/', 'HomeController@showWelcome');

Route::post('/', array(
    'before' => 'csrf',  // csrf filter
    'uses' => 'HomeController@sendEmail' // the controller action to be used
));

hello.php

<?php echo Form::open(array('action' => 'HomeController@sendEmail')) ?>
      <!-- input fields here -->
<?php echo Form::close() ?>

HomeController.php

Public function showWelcome()
{
    return View::make('hello');
}

public function sendEmail()
{
    $data = Input::all();
    print_r($data);
    // return the same view but with posted fields in the $data array
    return View::make('hello', $data);
}

OTHER TIPS

Routes

Route::get('/', 'HomeController@showWelcome');
Route::post('/', 'HomeController@sendEmail');

Hello.blade.php

@if(isset($post))
    {{$post}}
@endif
{{Form::open()}}
{{Form::text('sometext')}}
{{Form::close()}}

HomeController

Public function showWelcome()
{
    return View::make('hello');
}

public function sendEmail()
{

$post = Input::all();
return View::make('hello', array('post' => $post));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top