如何用Laravel框架创建一个注册模式


How to create a signup modal with Laravel Framework?

我有一个模态在我的布局中创建用户(作为注册选项),我可以正确地创建用户,但我需要登录它自动并将他重定向到另一个页面,我该怎么做?

下面是我的代码:

我的模态在页眉布局

<div id="LoginModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content" style="background-image: url(/assets/img/Login/Email_Gate.png);width: 420px;height: 550px;">
            <div class="modal-body" style="padding:0;">
                <div class="tabbable">
                    <ul class="nav nav-tabs" id="LoginNavUl" style="margin:50px;">
                        <li style="width: 150px;margin: 0; height: 47px;"><a href="#tab1" data-toggle="tab" aria-expanded="false" style="text-align: center;">Sign Up</a></li>
                        <li class="active" style="width: 150px;margin: 0;"><a href="#tab2" data-toggle="tab" aria-expanded="true" style="text-align: center;">Login</a></li>
                    </ul>
                    <div class="tab-content active">
                        <div class="tab-pane" id="tab1">
                            <div class="green-banner" style="background-color: #04c08d; height: 60px;">
                                <p>Nice to meet you.</p>
                            </div>
                            <div class="content-login" style="padding-left:70px; padding-right:70px; padding-top:50px;">
                                <form id="formRegister" class="form-horizontal" role="form" method="POST" action="{{ route('signup') }}">
                                    <input type="hidden" name="_token" value="{{ Session::token() }}">
                                    <input type="email" class="form-control" name="email" style="width: 100%;" placeholder="Email"><br/>
                                    <input type="password" class="form-control" name="password" style="width: 100%;" placeholder="Password"><br/>
                                    <input type="date" style="width: 100%; margin-bottom: 70px;" name ="eventdate" placeholder="Event Date"/><br/>
                                    <div class="btn-submit-login">
                                        <div>
                                            <button id="edit-submit" name="op"  type="submit" style="border: none;background-color: transparent;">SIGN UP</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我的控制器:

public function createUser(Request $request )                        
{                                                                         
$email = $request['email'];                                           
$password = bcrypt($request['password']);                             
$user = new UserModel();                                              
$user->email = $email;                                                
$user->password = $password;                                          
$user->role_id = 3;                                                   
$user->save();                                                        
/*
 I need to log the user here 
*/
if  user is logged in {    
    Redirect::to('/collection');                                      
} else {                                                              
    Redirect::to('/');                                                
}                                                                     

}

我认为这样的东西应该在你的控制器中工作:

public function createUser(Request $request )                        
{                                                                         
$email = $request['email'];                                           
$password = bcrypt($request['password']);                             
$user = new UserModel();                                              
$user->email = $email;                                                
$user->password = $password;                                          
$user->role_id = 3;                                                   
$user->save();
if  ( Auth::attempt(['email' => $email, 'password' => $password]) ) {    
    Redirect::to('/collection');                                      
} else {                                                              
    Redirect::to('/');                                                
}

https://laravel.com/docs/5.3/authentication authentication-quickstart

您可以登录用户并通过在您的createUser函数中保存用户之后添加以下两行代码来重定向他:

Auth::login($user);
Redirect::to('/collection');

为什么不使用Laravel内置的身份验证?

Laravel 5.3中你应该看到一个叫做app/Http/Controllers/Auth/RegisterController.php

的类

这个控制器默认使用一个称为RegistersUser的特性。这门课和你想的完全一样。它注册一个用户。你可以覆盖RegisterController.php类中的任何trait方法来添加任何你想要的功能。

您还有一个登录用户的LoginController.php,它甚至提供了一个称为authenticated()的方法,您可以在用户经过身份验证后在LoginController.php中重写该方法以执行各种操作。

作为旁注,我不确定你是否完全理解laravel的"要点",或者它实际上必须提供什么。你真的需要创建自己的UserModel吗?它内置了一个用户模型,该模型可以与框架的其余部分无缝协作——尤其是登录/注册类。在laravel中可以很容易地扩展和定制这么多东西的各个方面,我不明白你为什么要为这么多东西重新发明轮子。

我的建议是看一下文档@ https://laravel.com/docs/

看看laravel提供了什么,并尝试使用它。毕竟你在使用Laravel。如果你不打算利用Laravel所提供的功能,你可能会更好地通过composer引入Eloquent等,而不需要添加Laravel的重量。