分類
网站

使用laravel5自帶的用戶授權系統

我這個不是教程,只是記錄。目標是修改laravel5默認的用戶系統,使之能迅速使用。註冊時在原有信息基礎上添加手機號。至於搭建laravel看參見:在ubuntu上部署apache和laravel5

首先修改數據庫,添加phone列。由於我數據庫還是空的所以就先php artisan migrate:rollback,然後在database/migrations/2014_10_12_000000_create_users_table.php up函數中添加一行

$table->string('phone')->unique();

然後執行php artisan migrate這樣用戶數據庫就有phone了,而且phone還是唯一的。然後修改app/Http/Controllers/Auth/AuthController.php,主要是重載postRegister和postLogin兩個函數。原函數可從vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesAndRegistersUsers.php中複製過來,修改後是這樣的:

//app/Http/Controllers/Auth/AuthController.php
<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;

class AuthController extends Controller {



	use AuthenticatesAndRegistersUsers;
        //修改跳轉地址
        protected $redirectTo = '/';


	public function __construct(Guard $auth, Registrar $registrar)
	{
		$this->auth = $auth;
		$this->registrar = $registrar;

		$this->middleware('guest', ['except' => 'getLogout']);
	}

	public function postRegister(Request $request)
	{       
                //數據驗證
                $this->validate($request, ['phone'=>'required|numeric|unique:users',);
		$validator = $this->registrar->validator($request->all());

		if ($validator->fails())
		{
			$this->throwValidationException(
				$request, $validator
			);
		}

		$this->auth->login($this->registrar->create($request->all()));

		return redirect($this->redirectPath());
	}
        
        //這裏修改爲可以通過郵箱或手機登陸
        public function postLogin(Request $request)
	{
		
                $field = filter_var($request->input('login'), FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';
                $request->merge([$field => $request->input('login')]);
                $this->validate($request, [
			'login' => 'required', 'password' => 'required',
		]);

		$credentials = $request->only($field, 'password');

		if ($this->auth->attempt($credentials, $request->has('remember')))
		{
			return redirect()->intended($this->redirectPath());
		}

		return redirect($this->redirectPath())
					->withInput($request->only('email', 'remember'))
					->withErrors([
						'email' => $this->getFailedLoginMessage(),
					]);
	}
	protected function getFailedLoginMessage()
	{
		return '郵箱或手機與密碼不匹配。';
	}

}

然後修改下位於/home/zenggl/case1/resources/views/auth下的登陸和註冊的表單,登錄表單修改一下email輸入框的name爲login,註冊表單添加一個name爲phone的輸入框。

密碼重置功能laravel5中也是實現好的,只用配置一下郵件發送就好了,我以QQ企業郵箱爲例配置。首先在.env中填入郵件服務器信息,然後設置config中的mail就好了:

//.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.exmail.qq.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=yourPassword
//config/mail.php
//視郵箱服務器而定,QQ郵箱要用ssl加密,且發信人要和發信帳號一致
'from' => ['address' => '[email protected]', 'name' => null],
'encryption' => 'ssl',

修改提示語言爲簡體中文。首先修改config/app.index中的locale值爲zh-cmn-Hans,然後複製一份resources/lang/en並重命名爲zh-cmn-Hans,最後修改validation.php就可以了。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *