登录后进行操作 [英] Make an action after login

查看:77
本文介绍了登录后进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想在用户登录后向会话中添加一个变量(我使用Laravel 5及其artisan make:auth的身份验证.它看起来像这样:user->login->give_him_a_key->logout->delete_key.为此我制作了middleware但是我意识到当用户登录时我不能使middleware只运行一次,然后转到文件AuthenticateUser.php并将代码添加到函数login中.我的老师说这不好,因为我更改了laravel的代码.他让我尝试在不更改laravel的代码的情况下进行制作.

I have a problem that I want to add a variable to a session after user login (I use Laravel 5 and its auth by artisan make:auth. It looks like this: user->login->give_him_a_key->logout->delete_key. I make a middleware for this but I realize I cannot make middleware run only once when the user does a login, then I go to the file AuthenticateUser.php and add my code in function login. My teacher said that it is not good because I change the code of laravel. He asked me to try to make it without changing the code of laravel.

我尝试过,但是在用户登录后找不到如何捕获$request并使它仅运行一次.我需要一些指导.

I tried but cannot find how to catch $request after a user login and make it run only once. I need some guidance.

推荐答案

<? php

namespace App\ Http\ Middleware;

use Closure;

class AfterMiddleware {
  public

  function handle($request, Closure $next) {
    $response = $next($request);
    $login_route = "segement1/segement2..";
    $logout_route = "segement1/segement2..";

    if ($request - > is($login_route) && !$request - > session() - > has('key')) {
      $request - > session() - > put('key', 'value');
    }

    if ($request - > is($logout_route) && $request - > session() - > has('key')) {
      $request - > session() - > forget('key');
    }

    return $response;
  }
}

根据您的应用更改路线

这篇关于登录后进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆