Login and registration form in Laravel 5

mrakodol picture mrakodol · Oct 14, 2014 · Viewed 11.6k times · Source

I start to learn a new laravel and trying to learn it, by building small project, to make my starter site, but I have a problem. I only create new project without any plugin. I have a problem with login and register form. I put this into route file:

get('user/login', 'Auth\AuthController@showLoginForm');
post('user/login', 'Auth\AuthController@postLogin');
get('user/register', 'Auth\AuthController@showRegistrationForm');
post('user/register', 'Auth\AuthController@register');

And make a login and register form. It shows me a forms, but I can't submit it and I do not know how to insert into database or how to validate user credentials. Can you tell me how to do it in new beta Laravel framework?

Problem is that Laravel shows me a login and register form. But when I click submit it didn't do nothing, only refresh the page. I put method="POST" action="" into forms. When I change

public function register(RegisterRequest $request)

into

public function register()

in App\Http\Controllers\Auth\AuthController, when I submit form it give me an error that $request is not find.

You can see all code in: this link

Answer

Marcin Nabiałek picture Marcin Nabiałek · Oct 14, 2014

This is how it works in Laravel 5 when you use request. If you inject Request object for example this way:

public function register(RegisterRequest $request) {
   // your code goes here
}

the code of this function won't be executed until RegisterRequest validate your data with success.

Looking at your rules:

    return [
        'email' => 'required|email|unique:users',
        'password' => 'required|confirmed|min:8',
   ];

you need to make sure your data pass those rules.

You should also add to your form displaying errors:

@foreach ($errors->all() as $error)
 {{ $error }}
@endforeach

to display validation errors in form.

EDIT

Everything seems to be fine in your code. Valid function is being launched. If you don't believe me, change your register function into:

public function register()
{
   return "This is register function";
}

The error you get is because if you remove RegisterRequest $request from your function parameter, you cannot later use $request->email; because it's undefined