【问题标题】:How to flash a message using Laravel 5 Authentication?如何使用 Laravel 5 身份验证闪烁消息?
【发布时间】:2015-11-01 18:24:52
【问题描述】:

当有人使用 Laravel 的默认身份验证驱动程序登录时,我想闪烁一条消息。我知道我可以更改 AuthenticatesUsers.phpRegistersUsers.php 但它不会推送到 git。

有没有办法在 AuthController 中做到这一点?

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    如果您对 Laravel 处理身份验证的方式不满意,您可以使用自己的方法非常轻松地创建登录控制器。

    自定义登录控制器

    <?php
    
    namespace App\Http\Controllers\Auth;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Contracts\Auth\Guard;
    use Illuminate\Http\Request;
    
    class LoginController extends Controller
    {
        protected $auth;
    
        public function __construct(Guard $auth)
        {
            $this->auth = $auth;
        }
    
        public function getLogin()
        {
            // Load login view
            return view('auth.login');
        }
    
        public function postLogin(Request $request)
        {
            if ($this->auth->attempt($request->only('email', 'password')))
            {
                // Login is successful redirect user to wherever
                return redirect()->route('home');
            }
            
            // Login unsuccessful redirect back to form with errors
            return redirect()->route('login')
                    ->with( 'error', trans('account.incorrect_credentials'));
        }   
    }
    

    以上是一个简单的示例,您需要添加验证、Flash 消息等。

    路线

    Route::group(['namespace' => 'Auth','middleware' => 'guest'], function()
    {
        Route::get('login', array(
            'as'      => 'login',
            'uses'    => 'LoginController@getLogin'
        ));
        
        Route::post('login', array(
            'as'      => 'login.post',
            'uses'    => 'LoginController@postLogin'
        ));
    }
    

    您可能需要更改视图文件,以便表单发布到正确的路线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多