zoukankan      html  css  js  c++  java
  • Laravel 5.8 做个知乎 2 ——消息通知 laracasts/flash 未激活不可登录 与本地化overtrue/laravel-lang:~3.0

    1 消息通知

    1.1 安装与部署

    https://github.com/laracasts/flash

    composer require laracasts/flash

    configapp.php

    'providers' => [
    //。。。。 LaracastsFlashFlashServiceProvider
    ::class, ]

    1.2 使用

    修改模板

    esourcesviewslayoutsapp.blade.php

     <div class="container">
            @include('flash::message')
     </div>

    修改控制器 

    appHttpControllersAuthLoginController.php

    <?php
    
    namespace AppHttpControllersAuth;
    
    use AppHttpControllersController;
    use IlluminateFoundationAuthAuthenticatesUsers;
    use IlluminateHttpRequest;
    
    class LoginController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Login Controller
        |--------------------------------------------------------------------------
        |
        | This controller handles authenticating users for the application and
        | redirecting them to your home screen. The controller uses a trait
        | to conveniently provide its functionality to your applications.
        |
        */
    
        use AuthenticatesUsers;
    
        /**
         * Where to redirect users after login.
         *
         * @var string
         */
        protected $redirectTo = '/home';
    
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('guest')->except('logout');
        }
        
        /**
         *  重写方法
         */
        public function login(Request $request)
        {
            $this->validateLogin($request);
            
            // If the class is using the ThrottlesLogins trait, we can automatically throttle
            // the login attempts for this application. We'll key this by the username and
            // the IP address of the client making these requests into this application.
            if (method_exists($this, 'hasTooManyLoginAttempts') &&
              $this->hasTooManyLoginAttempts($request)) {
                $this->fireLockoutEvent($request);
               
                return $this->sendLockoutResponse($request);
            }
            
            if ($this->attemptLogin($request)) {
                //flash('欢迎回来','success');
                //flash('欢迎回来')->important();
               //flash('欢迎回来')->success();
                flash('欢迎回来')->overlay();
                return $this->sendLoginResponse($request);
            }
            
            // If the login attempt was unsuccessful we will increment the number of attempts
            // to login and redirect the user back to the login form. Of course, when this
            // user surpasses their maximum number of attempts they will get locked out.
            $this->incrementLoginAttempts($request);
            
            return $this->sendFailedLoginResponse($request);
        }
        
        /**
         * 重写登录验证方法
         * activated 必须为1 才能登录
         */
        protected function attemptLogin(Request $request)
        {
            $credentials = array_merge($this->credentials($request),['activated'=>1]);
            return $this->guard()->attempt(
              $credentials, $request->filled('remember')
            );
        }
    }
    View Code

    appHttpControllersEmailController.php

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use AppUser;
    use IlluminateSupportFacadesAuth;
    
    class EmailController extends Controller
    {
        //
        public function verify($token)
        {
            $user = User::where('activation_token',$token)->first();
            if(is_null($user)){
                flash('邮箱验证失败','danger');
                return redirect('/');
            }
            $user->activated = 1;
            $user->activation_token = str_random(40);
            $user->save();
            Auth::login($user);
            flash('邮箱验证成功,欢迎回来','success');
            return redirect('/home');
        }
    }
    View Code

    1.2.1 带x的提示栏

    flash('欢迎回来')->important();

    1.2.2 不带x的提示栏

    flash('欢迎回来')->success();

     flash('欢迎回来')->overlay();

    1.2.3 使用modal

    esourcesviewslayoutsapp.blade.php

    <script src="//code.jquery.com/jquery.js"></script>
    <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script>
        $(function () {
            $('#flash-overlay-modal').modal();
        });
    </script>
     flash('欢迎回来')->overlay();

     

    2 未激活不可登录

    appHttpControllersAuthLoginController.php

    use IlluminateHttpRequest;
        /**
         * 重写登录验证方法
         * activated 必须为1 才能登录
         */
        protected function attemptLogin(Request $request)
        {
            $credentials = array_merge($this->credentials($request),['activated'=>1]);
            return $this->guard()->attempt(
              $credentials, $request->filled('remember')
            );
        }

    修改登录提示

    esourceslangenauth.php

     'failed' => "账号错误或密码错误或邮箱未验证 These credentials do not match our records.",
        'throttle' => '登录尝试次数太多。请在 :seconds 秒后重试。 Too many login attempts. Please try again in :seconds seconds.',

    当activated=0时

    3 本地化

    3.1 翻译成中文

    参考

    https://blog.csdn.net/qq_35843527/article/details/88047687

    composer require "overtrue/laravel-lang:~3.0"

    configapp.php

    //'locale' => 'en',
        'locale' => 'zh-CN',
    'providers' => [
    
            //IlluminateTranslationTranslationServiceProvider::class,
            //TranslationServiceProvider改成TranslationServiceProvider
            OvertrueLaravelLangTranslationServiceProvider::class,    
    ]

    vendorlaravel-langlangjsonzh-CN.json

    中文翻译都在这个json里

    {
      "A fresh verification link has been sent to your email address.": "新的验证链接已发送到您的 E-mail。",
      "All rights reserved.": "版本所有。",
      "Before proceeding, please check your email for a verification link.": "在继续之前请先验证您的 E-mail。",
      "click here to request another": "点击重新发送 E-mail",
      "Confirm Password": "确认密码",
      "E-Mail Address": "E-mail",
      "Error": "错误",
      "Forbidden": "访问被拒绝",
      "Forgot Your Password?": "忘记密码?",
      "Go Home": "回首页",
      "Hello!": "您好:",
      "hi": "嗨",
      "If you did not create an account, no further action is required.": "如果您未注册帐号,请忽略此邮件。",
      "If you did not receive the email": "如果您没有收到",
      "If you did not request a password reset, no further action is required.": "如果您未申请重设密码,请忽略此邮件。",
      "If you’re having trouble clicking the ":actionText" button, copy and paste the URL below
    into your web browser: [:actionURL](:actionURL)": "如果您点击「:actionText」按钮时出现问题,请复制下方链接到浏览器中开启: [:actionURL](:actionURL)",
      "Login": "登录",
      "Logout": "注销",
      "Name": "姓名",
      "Oh no": "不好了",
      "Page Expired": "页面会话已超时",
      "Page Not Found": "页面不存在",
      "Password": "密码",
      "Please click the button below to verify your email address.": "请点击下面按钮验证您的 E-mail:",
      "Regards": "致敬",
      "Register": "注册",
      "Remember Me": "记住我",
      "Reset Password": "重设密码",
      "Reset Password Notification": "重设密码通知",
      "Send Password Reset Link": "发送重设密码链接",
      "Service Unavailable": "暂时不提供服务",
      "Sorry, you are forbidden from accessing this page.": "很抱歉,你没有权限访问此页面。",
      "Sorry, the page you are looking for could not be found.": "很抱歉!您浏览的页面不存在。",
      "Sorry, you are making too many requests to our servers.": "很抱歉!您向我们的服务器发出太多请求了。",
      "Sorry, you are not authorized to access this page.": "很抱歉!您没有权限浏览此页面。",
      "Sorry, your session has expired. Please refresh and try again.": "很抱歉!您的 Session 已过期,请刷新后再试一次。",
      "Sorry, we are doing some maintenance. Please check back soon.": "很抱歉!我们正在维护网站,请稍候再回来。",
      "Toggle navigation": "切换导航",
      "Too Many Requests": "太多请求",
      "Unauthorized": "未授权",
      "Verify Email Address": "验证 E-mail",
      "Verify Your Email Address": "验证 E-mail",
      "You are receiving this email because we received a password reset request for your account.": "您收到此电子邮件是因为我们收到了您帐户的密码重设请求。",
      "Whoops!": "哎呦!",
      "Whoops, something went wrong on our servers.": "哎呀,我们的服务器出了问题。",
      "This password reset link will expire in :count minutes.": "此密码重置链接将会在:count分钟内失效。"
    }

    3.2 修改时区

    configapp.php

        //'timezone' => 'UTC',
        'timezone' => 'Asia/Shanghai',

     4 上传代码

    git tag v2.0

    https://github.com/guainttt/laravel-zhihu/tags

     

  • 相关阅读:
    WTS_INFO_CLASS enumeration
    奇淫怪巧之给Delphi的PrintDialog增加一个页码选定范围打印的Edit
    Delphi的参数传递约定以及动态参数个数(转载笔记)
    C++中的模板那点事
    那些年我们一起学过的“排序算法”
    STL中的set容器的一点总结
    设计模式之备忘录模式(Memento)
    小程序员的趣味题(一)
    STL中的vector容器的一点总结
    STL中的list容器的一点总结
  • 原文地址:https://www.cnblogs.com/polax/p/14887797.html
Copyright © 2011-2022 走看看