Laravel Form POST redirect failure

1.9k Views Asked by At

I'm having a very frustrating (super-newbie) experience with Laravel 5.1. Sure there must be something that I'm missing, but unfortunately I couldn't find anything on Laravel docs.

The problem is this (and I believe is even relatively simple): while all GET routes are working, the routes in POST are 'rerouting' me to the wrong place. For instance, assuming that the controller is this:

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use View;

class PagesController extends BaseController {


  public function hello(){
    return view('hello');

  }




 public function register(){
    //Registration rules
    $inputData=array(
            'name' => \Input::get('name'),
             'email' =>\Input::get('email'),
             'password' => bcrypt(\Input::get('password')),
        );  


        createUser($inputData);

    return view('stat');
  }     




 private function createUser($inputData){
    return User::create($inputData);
  }

}

and given the route:

Route::get('/','PagesController@hello');

this one redirects me correctly to the view specified in the controller,at the method indicated.

A POST operation cannot be successfully performed, since

Route::post('/','PagesController@register');

with the form having:

<form action="/" method="post">
        <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        <label for="name">User Name</label><br/>
        <input type="text" name="name" id="name"/><br/>
        <label for="name">E-mail</label><br/>
        <input type="text" name="email" id="email"/><br/>
        <label for="password">password</label><br/>
        <input type="password" name="password" id="password"/><br/>
        <input type="submit" value="auth!"/>
    </form>

results in redirecting me to xampp home page. For example, the "main registration page" is at this url:

http://localhost:8080/laravel/project1/public/

(GET OK) and when I click on the submit button, I'm sent to

http://localhost:8080/

Just few more indicators for you to try and help me (I'm genuinely stuck): 1) I didn't use a VirtualHost (searched on the web, but had always no luck in configuring apache properly...good luck I've got xampp) 2) The page "hello" has a link that I used as a test:

<a href="stat">Link</a>

and it redirects me correctly to the sought-after

http://localhost:8080/laravel/project1/public/stat

3)In the POST method, I've used a dd($inputData) to see what was going wrong, and as a result I had...nothing. Not a blank page,just always the localhost page. This led me to think that somehow the controller method isn't called, since no dd(---) result got in page. Hope someone can help. Many thanks

2

There are 2 best solutions below

1
On

Your domain is http://localhost:8080 , so when you set action to '/' , it goes to root. It`s not good practice , but your solution is to change :

<form action="/laravel/project1/public/" method="post">

I`d create a virtual host for the project in apache , so document root would be /laravel/project1/public/ .

1
On

you are posting your form to the root / that is why you are being routed to xampp page. To solve this problem, change your form action to this

<form action="/post/my/form" method="post">

in your routes

Route::post('/post/my/form','PagesController@register');