Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

samer's avatar
Level 1

Redirect after login error

why i got an error ?!! In AuthController : protected $redirectTo = '{name}'; In Route file :Route::get('{name}', [ 'uses' =>'User\UserController@getprofile', 'as'=>'user.profile', 'middleware'=>'auth' ]);

0 likes
33 replies
Snapey's avatar

protected $redirectTo = '{name}';

What do you expect this to do?

Thijmen's avatar

You can make:

protected $redirectTo = route('user.profile');
samer's avatar
Level 1

@Thijmen FatalErrorException in AuthController.php line 32: syntax error, unexpected '(', expecting ',' or ';'

d3xt3r's avatar

@Thijmen PHP does not support property default value to be set this way. You need to initialise them in constructor.

@samer Start looking at method that does the redirection, and override it to handle the dynamic redirect. And by the way

 Route::get('{name}', // This route will digest all other routes so change this to something more appropriate.
samer's avatar
Level 1

i made this in AuthController: protected $redirectTo = route('user.profile'); and this :Route::get('{name}', [ 'uses' =>'User\UserController@getprofile', 'as'=>'user.profile', 'middleware'=>'auth' ]); in routes file and i got an error!!

d3xt3r's avatar

Use a good ide for PHP, you would be able to see the error and the explanation.

samer's avatar
Level 1

FatalErrorException in AuthController.php line 32: syntax error, unexpected '(', expecting ',' or ';'

bashy's avatar

@samer You want to redirect to their profile or username on login? Nowhere in this thread have you really said what you want to do.

samer's avatar
Level 1

yeah , i want to redirect users to their profile.

bashy's avatar

@samer Then you will want to override this method within the AuthController.

I believe the first one will work... intended() with a route() inside...

protected function authenticated($request, $user)
{
    // redirect to profile only if they didn't get redirected back from an auth middleware page.
    return redirect()->intended(route('user.profile', $user->name));

    // redirect to profile regardless of where they wanted to go.
    return redirect()->route('user.profile', $user->name);
}
samer's avatar
Level 1

i'm using laravel 5.2 auth

Thijmen's avatar

@samer You provide no information at all. What is your result? Do you see any errors?

samer's avatar
Level 1

FatalErrorException in AuthController.php line 32: syntax error, unexpected '(', expecting ',' or ';'

Thijmen's avatar

It would help enormously if you actually read the things we post here.

Try this;

protected $redirectTo  = '';
public function __construct() {
    $this->redirectTo = route('users.profile');
}
d3xt3r's avatar

Why bother :( Instead of jumping to coding straight away, read books, go through some tutorials, will make life easy

samer's avatar
Level 1

it work but when i went to account page ErrorException in AuthController.php line 42: Trying to get property of non-object

Thijmen's avatar

Look @samer, it would help if you showed us whats on line 42 of AuthController.php...

samer's avatar
Level 1

public function __construct() { $this->middleware($this->guestMiddleware(), ['except' => 'logout']); $this->redirectTo = route('user.profile',Auth::user()->name); //line 42

}
bashy's avatar

You can't load auth()->user()->name since __construct() is loaded BEFORE you've authed (which is why I suggested overriding the other method that is called upon logging in).

Next

Please or to participate in this conversation.