js091514
js091514

Reputation: 165

laravel form not directing as expected

I am creating a registration in laravel.

I am expecting this form on submit to take me to (the same page I am on) http://localhost:8888/mylaravel/notetomyself/public/auth/register

but on submitting it actually ends up at http://localhost:8888/auth/register

The following code is my view in views/auth/register.blade.php

<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Register</div>
                <div class="panel-body">

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<form class="form-horizontal" role="form" method="POST" action="/auth/register">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ old('name') }}">
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
</div>
</div>

<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
    Register
</button>
</div>

</div>
</form>
</div>
</div>
</div>
</div>
</div>

my routes.php file contains this:

Route::get('login', array('uses' => 'HomeController@showLogin'));
Route::get('home', array('uses' => 'Home@index'));
Route::post('login', array('uses' => 'HomeController@doLogin'));
Route::get('logout', array('uses' => 'HomeController@doLogout'));
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::controllers([
   'password' => 'Auth\PasswordController',
]);

The controller auth/AuthController.php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    private $redirectTo = '/';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

Wondering if there is somewhere I might have set the wrong paths?

Upvotes: 2

Views: 122

Answers (2)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

Why you are redirected there :

Its because you form action points to /auth/register [As it is without full path web server simply treats as your domain name as root path].

What you should do :

Simply remove the / before auth in action="/auth/register"

Additional Tip : (To do after fix)

To handle the redirection after/before authentication you should customize according to your change in Authenticate.php and RedirectIfAuthenticated.php inside app/Http/Middleware either by return redirect()->guest('yourpath'); or to let in the request return $next($request);

Note :

Change the action according to your routes !

Upvotes: 1

Thomas Kim
Thomas Kim

Reputation: 15931

As the others have said, your path is relative to the root domain. The simplest solution is to use Laravel's URL helper functions to create an absolute URL. You have several options.

  1. Generate an absolute URL to the given path:

    action="{{ url('auth/register') }}"
    
  2. Generate an absolute URL using a route name

    action="{{ route('routeName') }}"
    
  3. Generate an absolute URL specifying a controller action:

    action="{{ action('Auth\AuthController@postRegister') }}"
    

Source: http://laravel.com/docs/5.1/helpers#urls

Upvotes: 1

Related Questions