Doo Doo
Doo Doo

Reputation: 1347

Cannot redirect to a path in Laravel 5.2

In laravel 5.1 I have overwrite postRegister then redirect it to a path and it worked well, but in Laravel 5.2 I write the same code it didn't work. here some of my code

public function postRegister(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        $this->create($request->all());

        return redirect('login');
    }

this I want to redirect to page login after user register.

I got the problem like this:

ErrorException in SessionGuard.php line 411: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

Note: I used laravel default scafold

Thanks in advance

Upvotes: 3

Views: 1549

Answers (2)

Clément Rigo
Clément Rigo

Reputation: 430

postRegister() is no longer the method to override. Use this instead:

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::guard($this->getGuard())->login($this->create($request->all()));

    return redirect(route('route.after.register'));
}

Don't forget to import the Auth Facade:

use Illuminate\Support\Facades\Auth;

Upvotes: 0

Nauphal
Nauphal

Reputation: 6192

Is your routes inside the web middleware ? Laravel 5.2 has a new feature called middleware groups. In order to use session, your route should be inside the web middleware. If you check your Kernel.php, you will get a clear idea about the middlewares inside the web middleware group

    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

Upvotes: 0

Related Questions