在 Laravel 中注册后重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31102564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:51:08  来源:igfitidea点击:

Redirect after register in Laravel

phplaravelregistration

提问by shaNnex

I am using Laravel 5.

我正在使用 Laravel 5。

I wanted to redirect a user after a successful registration. I have tried these in my authcontroller but it doesn't work.

我想在成功注册后重定向用户。我已经在我的 authcontroller 中尝试过这些,但它不起作用。

protected $loginPath = '/plan';
protected $redirectTo = '/plan';
protected $redirectAfterRegister = 'plan/';

These work on successful login though but not after registration.

这些虽然可以成功登录,但不能在注册后使用。

I have also tried postRegisterto render a view but using a postRegistermethod overrides the registration process and I don't want to do that. I just want to redirect the user to a page on successful registration.

我也尝试postRegister渲染视图,但使用postRegister方法覆盖了注册过程,我不想这样做。我只想将用户重定向到成功注册的页面。

回答by Francesco de Guytenaere

Overriding the postRegister function like you mention should work, you would do this in your AuthController:

像你提到的那样覆盖 postRegister 函数应该可以工作,你可以在你的AuthController

 public function postRegister(Request $request)
 {
    $validator = $this->registrar->validator($request->all());
    if ($validator->fails())
    {
        $this->throwValidationException(
            $request, $validator
        );
    }
    $this->auth->login($this->registrar->create($request->all()));     
    // Now you can redirect!
    return redirect('/plan');
 }

Or something like it. Copy it from the AuthenticatesAndRegistersUsersthat is used in the top of your AuthController, here you will find all the functions

或者类似的东西。从AuthenticatesAndRegistersUsers你的顶部使用的复制它AuthController,在这里你会找到所有的功能

For this to work your AuthControllershould use the 'AuthenticatesAndRegistersUsers' trait, which is there by default.

为此,您AuthController应该使用默认情况下存在的“AuthenticatesAndRegistersUsers”特征。

More info about redirects in case you want to redirect to a named route: http://laravel.com/docs/5.0/responses#redirects

如果您想重定向到命名路由,有关重定向的更多信息:http: //laravel.com/docs/5.0/responses#redirects

回答by Shankar Prakash G

Simply add below line to AuthController class in Auth/AuthController.php

只需将以下行添加到 Auth/AuthController.php 中的 AuthController 类

protected $redirectPath= '/plan';

Above redirect path will be used for successful login and successful register.

以上重定向路径将用于成功登录和成功注册。

回答by FJ_

You can also modify the return of register() in RegisterUsers.php:

也可以修改RegisterUsers.php中register()的返回:

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()));
    // Originally the parameter is $this->redirectPath()
    return redirect()->to('/plans');
}

回答by Farshad Fahimi

after run command

运行命令后

php artisan make:auth

laravel create auth controller and resource files for change the route of register user just go to below path

laravel 创建 auth 控制器和资源文件以更改注册用户的路径只需转到以下路径

App\Http\Controllers\Auth\RegisterController

and change the protected $redirectToparam and you can user this way in LoginControllernext of RegisterControllerfor redirect after login

并更改protected $redirectTo参数,您可以 在登录后重定向的LoginController下一步中使用这种方式RegisterController

回答by Abdul Manan

Here is laravel 5.4 solution

这是laravel 5.4解决方案

     /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {

          //User register now here you can run any code you want 
          if (\Request::ajax()){

            return response()->json([
                'auth' => auth()->check(),
                'intended' => $this->redirectPath(),
            ]);
            exit();
        }
        return redirect($this->redirectPath());
    }

keep in mind register()Handle a registration request for the application. where registered()called when user created.

请记住register()处理应用程序的注册请求。registered()用户创建时调用的地方。