laravelµÇ¼ʵÏÖ
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶àÇ¿Ê¢µÄ¹¤¾ßºÍ¹¦Ð§£¬×ÊÖú¿ª·¢ÕßÇáËɵع¹½¨webÓ¦ÓóÌÐò¡£ÆäÖУ¬ÊµÏֵǼ¹¦Ð§ÊÇ¿ª·¢Ó¦ÓóÌÐòµÄÖ÷ÒªÒ»²½¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖÐʵÏֵǼ¹¦Ð§¡£
×°ÖÃLaravel
Laravel¿ÉÒÔͨ¹ýComposer×°Öã¬ComposerÊÇÒ»¸öPHPÒÀÀµÖÎÀí¹¤¾ß¡£×°ÖÃComposer£¬ÊäÈëÒÔÏÂÏÂÁ
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÀ´½¨Éè Laravel Ó¦ÓóÌÐò¡£
composer create-project --prefer-dist laravel/laravel projectname
µÇ¼ºó¸´ÖÆ
Êý¾Ý¿âÉèÖÃ
ÔÚʹÓÃLaravelµÄÈÏ֤ϵͳ֮ǰ£¬ÐèÒªÉèÖÃÊý¾Ý¿â¡£·¿ª .env Îļþ£¬¸ü¸ÄÒÔÏÂʾÀýÖеÄÊý¾Ý¿âÅþÁ¬ÉèÖá£
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=mydatabase DB_USERNAME=root DB_PASSWORD=
µÇ¼ºó¸´ÖÆ
ÌìÉúÓû§ÈÏ֤ϵͳ
ÔËÐÐÒÔÏÂÏÂÁîÌìÉú Laravel µÄÓû§ÈÏ֤ϵͳ¡£
php artisan make:auth
µÇ¼ºó¸´ÖÆ
Õ⽫½¨ÉèÒ»¸öÓû§×¢²á¡¢µÇ¼ºÍ×¢ÏúµÄ»ù±¾ÊÓͼºÍ¿ØÖÆÆ÷¡£
½¨ÉèÓû§Ä£×Ó
½ÓÏÂÀ´£¬ÐèÒª½¨ÉèÒ»¸öÓû§Ä£×Ó£¬LaravelĬÈÏÌṩµÄÓû§Ä£×ÓÊÇAppUser£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉè¡£
php artisan make:model User
µÇ¼ºó¸´ÖÆ
×¢²áºÍµÇ¼ÑéÖ¤
×¢²áÑéÖ¤£º
public function store(Request $request) { $this->validate($request, [ 'name' => 'required', 'email' => 'required|email|unique:users,email', 'password' => 'required|min:6|confirmed', ]); $user = User::create([ 'name' => $request['name'], 'email' => $request['email'], 'password' => bcrypt($request['password']), ]); Auth::login($user); return redirect()->route('dashboard'); }
µÇ¼ºó¸´ÖÆ
µÇ¼ÑéÖ¤£º
public function login(Request $request) { $this->validate($request, [ 'email' => 'required|email', 'password' => 'required|min:6', ]); $credentials = [ 'email' => $request['email'], 'password' => $request['password'], ]; if (Auth::attempt($credentials)) { return redirect()->intended('dashboard'); } else { return redirect()->back()->withInput($request->only('email', 'remember')); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬attemptÒªÁ콫ÑéÖ¤Óû§Æ¾Ö¤£¬ÈôÊÇͨ¹ýÑéÖ¤£¬½«»á½«Óû§×´Ì¬Í¨¹ýsessionÉúÑÄ£¬²¢ÇÒ¿ÉÒÔʹÓÃAuth::checkÒªÁì¼ì²éÓû§ÊÇ·ñÒѵǼ¡£
ÖÐÐļþ
ÖÐÐļþÌṩÁ˶ÔÓ¦ÓóÌÐòµÄ½øÒ»²½¿ØÖƺͱ£»¤¡£LaravelÒѾԤ½ç˵ÁËÐí¶àÖÐÐļþ£¬°üÀ¨ÑéÖ¤Óû§ÊÇ·ñµÇ¼µÄÖÐÐļþ¡£
ÔÚÓ¦ÓóÌÐòÖÐÌí¼ÓÈÏÖ¤ÖÐÐļþ£¬²¢½«ÆäÓ¦ÓÃÓÚÐèÒª±£»¤µÄ·ÓÉ£º
Route::middleware(['auth'])->group(function () { Route::get('/dashboard', 'DashboardController@index')->name('dashboard'); });
µÇ¼ºó¸´ÖÆ
Ç¿ÖÆʹÓà HTTPS
ÔÚLaravelÖУ¬¿ÉÒÔÇáËɵؽ«Ó¦ÓóÌÐòÇ¿ÖÆʹÓÃHTTPS£º
ÔÚ.envÖÐÌí¼ÓÒÔÏÂÐУº
APP_ENV=production APP_URL=https://example.com
µÇ¼ºó¸´ÖÆ
È»ºó£¬·¿ªAppServiceProviderÀ࣬Ìí¼ÓÒÔÏÂÐУº
public function boot() { if (config('app.env') === 'production') { URL::forceScheme('https'); } }
µÇ¼ºó¸´ÖÆ
Õ⽫ǿÖƽ«Ó¦ÓóÌÐòµÄURLÁ´½Óת»»ÎªHTTPSÐÒé¡£
×ܽá
ÖÁ´Ë£¬±¾ÎÄÏÈÈÝÁËÔõÑùÔÚLaravelÖÐʵÏֵǼ¹¦Ð§£¬°üÀ¨×°ÖÃLaravel¡¢ÉèÖÃÊý¾Ý¿â¡¢ÌìÉúÓû§ÈÏ֤ϵͳ¡¢½¨ÉèÓû§Ä£×Ó¡¢×¢²áºÍµÇ¼ÑéÖ¤¡¢ÖÐÐļþºÍÇ¿ÖÆʹÓÃHTTPS¡£Ê¹Óñ¾ÎÄÌṩµÄ°ì·¨À´ÊµÏÖLaravelµÄµÇ¼¹¦Ð§£¬¿ÉÒÔ×ÊÖúÄú¹¹½¨³öÔ½·¢Çå¾²ºÍ¹¦Ð§¸»ºñµÄÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇlaravelµÇ¼ʵÏÖµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡