Laravel 5.2 Route :: Aut()我正在尝试实施额外的电子邮件确认,在最后一步中,我无法在退出时传递一条Flash消息

时间:2016-02-25 01:36:32

标签: laravel-5.2

问题是在用户确认电子邮件并完成注册后,我无法发送flash(),reflash()或keep()消息。我在路线:Auth()中将其重定向到laravel 5.2中的/ logout。

我尝试确认用户的电子邮件(此部分没有问题),但我无法向“欢迎”视图发送消息。我想在确认用户需要再次登录后再注销用户。我发现注销的唯一方法是重定向('/ logout')并注销重定向到'welcome'视图,这对我来说没问题,但是我无法向视图发送消息。

    namespace TutorialJD\Http\Controllers;

use Illuminate\Http\Request;

use TutorialJD\Http\Requests;
use TutorialJD\Http\Controllers\Controller;

use TutorialJD\User;
use Auth;
use Mail;
use Session;

class LogController extends Controller
{
  public function confirm($email, $confirm)
  {
    $user = new User;
    $confirm_user= $user->select()->where('email', '=', $email)->where('confirm', '=', $confirm)->get();
    if(count($confirm_user)>0){
      $active = 1;
      $confirm = str_random(100);
      $user->where('email', '=', $email)->update(['active'=> $active, 'confirm'=> $confirm]);
      Session::reflash('message', "The email: $email has been confirmed, now you can access to Dashboard");
      return redirect('/logout');
    }
    else {
      return redirect()->route('welcome');
    }
  }

}

这些是proyect的其他部分:

迁移:

    public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        //to check email confirmation
        $table->boolean('active')->default(0);
        $table->string('confirm', 100)->nullable();;
        //token to make a route to confirm email
        $table->rememberToken();
        $table->timestamps();
    });
}

AuthController:

protected function create(array $data)
{
    $send['email'] = $data['email'];
    $send['name'] = $data['name'];
    $send['confirm'] = $data['confirm'];
    Mail::send('mails.register', ['send'=> $send], function($mail) use($send){
      $mail->to($send['email'], $send['name'])->subject('Please, to confirm your email, click in this link');
    });

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'confirm' => $data['confirm'],
        'active' => 0,
    ]);

}

我让LogController确认电子邮件,我想做一个用户注销并向欢迎视图发送一条flash消息,例如“你必须再次登录才能转到仪表板”。

routes.php文件:

Route::group(['middleware' => 'web'], function () {
    Route::get('/', ['as'=> 'welcome', function () {
        return view('welcome');
    }]);
    Route::auth();
});

Route::group(['middleware' => ['web', 'active']], function () {
    Route::get('/home',[
      'as'=> 'home',
      'uses' => 'HomeController@index'
    ]);
});

Route::get('auth/confirm/email/{email}/confirm/{confirm}', [
  'as'=> 'confirm',
  'uses'=> 'LogController@confirm'
  ]);

还有一个活跃用户的中间件。

namespace TutorialJD\Http\Middleware;

use Closure;
use Auth;

class Active
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
      if(Auth::check()){
        if(Auth::user()->active === 1){
          return $next($request);
        }
        else{
          $email= Auth::user()->email;
          $msg= session()->flash('message', "You must confirm your email to access.");
          return redirect()->route('welcome');
        }
      }
      else{
        return redirect()->route('welcome');
      }
    }
}

欢迎视图(当我在控制器中进行重定向('\ logout')时,Route :: Auth()显示视图('welcome'):

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Welcome</div>

                <div class="panel-body">
                    Your Application's Landing Page.
                    @if(Session::has('message'))
                      <br>
                      <strong>{{ Session::get('message') }}</strong>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

我认为问题是在注销和消息消息后会话文件是空的,中间件的消息工作正常但是注销后的消息不起作用。

0 个答案:

没有答案