简体   繁体   English

Laravel 5 - 注册后重定向到 URL

[英]Laravel 5 - Redirect To URL After Registration

have multiple users with totally different post login panels.有多个用户具有完全不同的登录后面板。 One for teachers and other for students.一份给老师,一份给学生。 I made middlewares to separate their views.我做了中间件来区分他们的观点。 Middleware checks if a property ( column in database ) named entity is 'student' or 'company' and restricts or permits views accordingly.中间件检查属性(数据库中的列)命名实体是“学生”还是“公司”,并相应地限制或允许视图。 And yes Guest isnt permitted to either of the post login panel.是的,访客不允许进入任何一个登录后面板。

I am using same table to save logins.我正在使用同一个表来保存登录信息。 (entity column differentiates if its a teacher or student). (实体列区分是老师还是学生)。 Now I want to redirect the users to different views.现在我想将用户重定向到不同的视图。 If I alter $redirectTo = "/studentPanel" .如果我改变$redirectTo = "/studentPanel" Middleware acts and teacher login cannot access this .中间件行为和教师登录无法访问此。 But if I hard code $redirectTo = "/teacherPanel" , then student post login panel is not accesible.但是如果我硬编码$redirectTo = "/teacherPanel" ,那么学生帖子登录面板是不可用的。

How can set $redirectTo dynamically .如何动态设置$redirectTo I thought of setting in construct method of auth controller.我想到了在auth控制器的构造方法中进行设置。

Tried this:试过这个:

public function __construct(Request $request ) {

$this->middleware('guest', ['except' => 'getLogout']);

if(Auth::user() && $request->user()->isStudent() )
    $this->redirectTo = "/studentPanel";
elseif(Auth::user() && $request->user()->isTeacher() )
    $this->redirectTo = "/teacherPanel";
else
      $this->redirectTo = "/auth/login";

} }

Here isStudent() and isCompany are functions in App\\User which respond with true or false checking the entity column value in Database.这里的isStudent()和 isCompany 是 App\\User 中的函数,它们以 true 或 false 检查数据库中的实体列值作为响应。

I thought this way and I am getting error "Call to a member function isStudent() on a non-object"我是这样想的,我收到错误"Call to a member function isStudent() on a non-object"

You need to change $request->user() to Auth::user() .您需要将$request->user()更改$request->user() Auth::user()

From there, I have faced a very similar issue and the best solution I found at the time was to set $redirectTo to a static value, and in the route you find whether the user is a student or teacher and redirect appropriately.从那里,我遇到了一个非常相似的问题,当时我找到的最佳解决方案是将$redirectTo设置$redirectTo静态值,并在路由中找到用户是学生还是教师并适当重定向。

The route would look like this:路线如下所示:

public function getRedirect()
{

    if (Auth::user() && Auth->user()->isStudent()) {
        return redirect("/studentPanel");
    } else if(Auth::user() && Auth->user()->isTeacher()) {
        return redirect("/teacherPanel");
    } else {
        return redirect("/auth/login");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM