2016-09-12 25 views
1

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

这里是我的代码:

我的头布局模式

<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('/');             
}                  

}

+0

https://laravel.com/docs/5.3/authentication –

回答

0

您可以登录用户,并通过添加重定向他这两行代码在你将用户保存在你的0之后功能:

Auth::login($user); 

Redirect::to('/collection'); 
+0

为此,请确保用户模型使用'Authenticatable'。 OP似乎使用他自己的用户模型 – Pistachio

0

我觉得这样的事情应该在你的控制器工作:

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

0

你为什么不使用Laravel的内置认证?

Laravel 5.3你将会看到一个叫做app/Http/Controllers/Auth/RegisterController.php

类这个控制器使用了一个名为RegistersUser默认特质。这门课完全符合你的想法。它注册一个用户。 您可以覆盖您的RegisterController.php类中的任何特征方法,以添加所需的任何功能。

你也有一个LoginController.php它记录在用户,它甚至提供了一个名为authenticated()方法,你可以在你的LoginController.php覆盖做各种东西用户已通过验证之后。

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

我的建议是看看文件@https://laravel.com/docs/

看到了什么laravel提供,并尝试使用它。你毕竟使用Laravel。如果你不打算利用Laravel所提供的东西,那么通过作曲家来拉动Eloquent等可能会更好,而不会增加Laravel的重量。