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

kendrick's avatar

Sorry for coming back @bugsysha

I tried to test adding the role to the route within the RouteServiceProvider, as below

public const HOME = '/home/' . auth()->user()->getType();

which results, logically, into a Constant expression contains invalid operations.

Is there a way to get around this? Using one home route for both users seems to be too complex. I tried to add the user type to the HOME const, so that we would get redirected to e.g. /home/doctor

bugsysha's avatar

Move it to public static method and all good :)

kendrick's avatar

RouteServiceProvider:

public static HOME = '/home/' . auth()->user()->getType();

will result in: syntax error, unexpected 'HOME' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)

bugsysha's avatar
public static function home(): string
{
    return '/home/' . auth()->user()->getType();
}

But that should blow up when using terminal since there will be no request object.

kendrick's avatar

You are right. It also requests HOME, as my $redirectTo is protected $redirectTo = RouteServiceProvider::HOME, when I add

RouteServiceProvider:

// public static HOME = '/home/' . auth()->user()->getType();

public static function home(): string
{
    return '/home/' . auth()->user()->getType();
}

There seems to be no workaround, the problem is e.g. when a doctor verifies an email, he would be redirected to /home, instead of /home/doctor, as the system is connected to $redirectTo

bugsysha's avatar

Why do you need to have home/doctor? That is just home for that user. URL should not depend on user type. Then you would have to remember all types that you have in your system when you talk with other colleagues so they would know which one you are talking about?

Use polymorphic views when you return view in controller or something like that, but I would also make that to return home view.

kendrick's avatar

You are right @bugsysha

If I have many collections, e.g. 5 for users, and 8 for doctors, within the HomeController@home, added to the view through compact(''). If I then use polymorphic views, will they only trigger the collections for each type of user, or when I visit /home, as a user, will I also load the 8 collections of doctors. Just thinking about performance, here.

bugsysha's avatar

If you are worried about performance of few collections and views which are compiled then you are worrying about wrong thing. Use something like telescope or debugbar and see if that changes your response time at all.

You need to have far greater complexity in order to bring servers to their knees these days. Worry about queries and stuff like that since they take most time.

1 like
Previous

Please or to participate in this conversation.