日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Laravel大型项目系列教程(二)之用户管理

發(fā)布時(shí)間:2024/4/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Laravel大型项目系列教程(二)之用户管理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Laravel大型項(xiàng)目系列教程(二)

一、前言

本節(jié)教程將大概實(shí)現(xiàn)用戶的注冊(cè)、修改個(gè)人信息、管理用戶功能


二、Let's go

1.創(chuàng)建用戶注冊(cè)視圖

<span style="font-size:14px;">$ php artisan generate:view users.create</span>

修改app/views/users/edit.blade.php:

@extends('_layouts.default')@section('main')<div class="am-g am-g-fixed"><div class="am-u-lg-6 am-u-md-8"><br/>@if (Session::has('message'))<div class="am-alert am-alert-{{ Session::get('message')['type'] }}" data-am-alert><p>{{ Session::get('message')['content'] }}</p></div>@endif@if ($errors->has())<div class="am-alert am-alert-danger" data-am-alert><p>{{ $errors->first() }}</p></div>@endif{{ Form::open(array('url' => 'register', 'class' => 'am-form')) }}{{ Form::label('email', 'E-mail:') }}{{ Form::email('email', Input::old('email')) }}<br/>{{ Form::label('nickname', 'NickName:') }}{{ Form::text('nickname', Input::old('nickname')) }}<br/>{{ Form::label('password', 'Password:') }}{{ Form::password('password') }}<br/>{{ Form::label('password_confirmation', 'ConfirmPassword:') }}{{ Form::password('password_confirmation') }}<br/><div class="am-cf">{{ Form::submit('Register', array('class' => 'am-btn am-btn-primary am-btn-sm am-fl')) }}</div>{{ Form::close() }}<br/></div></div> @stop

修改layouts/nav.blade.php中的@else部分:

@else<div class="am-topbar-right"><a href="{{ URL::to('register') }}" class="am-btn am-btn-secondary am-topbar-btn am-btn-sm topbar-link-btn"><span class="am-icon-pencil"></span> Register</a></div><div class="am-topbar-right"><a href="{{ URL::to('login') }}" class="am-btn am-btn-primary am-topbar-btn am-btn-sm topbar-link-btn"><span class="am-icon-user"></span> Login</a></div> @endif

在routes.php中增加

Route::get('register', function() {return View::make('users.create'); });

啟動(dòng)開(kāi)發(fā)服務(wù)器,瀏覽器中訪問(wèn)localhost:8000,導(dǎo)航條中多了一個(gè)Register按鈕:

點(diǎn)擊Register按鈕,進(jìn)入用戶注冊(cè)頁(yè)面:

2.實(shí)現(xiàn)用戶注冊(cè)

在routes.php中增加:

Route::post('register', array('before' => 'csrf', function() {$rules = array('email' => 'required|email|unique:users,email','nickname' => 'required|min:4|unique:users,nickname','password' => 'required|min:6|confirmed',);$validator = Validator::make(Input::all(), $rules);if ($validator->passes()){$user = User::create(Input::only('email', 'password', 'nickname'));$user->password = Hash::make(Input::get('password'));if ($user->save()){return Redirect::to('login')->with('message', array('type' => 'success', 'content' => 'Register successfully, please login'));} else {return Redirect::to('register')->withInput()->with('message', array('type' => 'danger', 'content' => 'Register failed'));}} else {return Redirect::to('register')->withInput()->withErrors($validator);} }));

上面表單驗(yàn)證規(guī)則的unique:users,email能確保users表中的email字段是唯一的,切記users和email之間不能有空格,confirmed確保提交的數(shù)據(jù)必須有一個(gè)名為password_conformation的字段且與password字段的值相等。

例如當(dāng)輸入已存在的email時(shí),會(huì)出現(xiàn)錯(cuò)誤提示:

之后我們?cè)傩薷膬蓚€(gè)地方,把routes.php中post login內(nèi)的


return Redirect::to('login')->withInput()->with('message', 'E-mail or password error');

修改為:


return Redirect::to('login')->withInput()->with('message', array('type' => 'danger', 'content' => 'E-mail or password error'));

把login.blade.php中的

@if (Session::has('message'))<div class="am-alert am-alert-danger" data-am-alert><p>{{ Session::get('message') }}</p></div> @endif

修改為

@if (Session::has('message'))<div class="am-alert am-alert-{{ Session::get('message')['type'] }}" data-am-alert><p>{{ Session::get('message')['content'] }}</p></div> @endif

現(xiàn)在你就可以嘗試注冊(cè),如果注冊(cè)成功就會(huì)跳轉(zhuǎn)到登錄頁(yè)面,并給出成功的提示:

注冊(cè)成功之后你可以試試是否能用剛注冊(cè)的賬號(hào)成功登錄。

3.修改個(gè)人信息

用戶注冊(cè)之后我們還應(yīng)該讓他能夠修改信息,在_layouts/nav.blade.php中添加修改個(gè)人信息的選項(xiàng):

<li><a href="{{ URL::to('user/'. Auth::id() . '/edit') }}"><span class="am-icon-user"></span> Information</a></li>

添加視圖users/edit.blade.php

$ php artisan generate:view users.edit

修改users/edit.blade.php:

@extends('_layouts.default')@section('main')<div class="am-g am-g-fixed"><div class="am-u-lg-6 am-u-md-8"><br/>@if (Session::has('message'))<div class="am-alert am-alert-{{ Session::get('message')['type'] }}" data-am-alert><p>{{ Session::get('message')['content'] }}</p></div>@endif@if ($errors->has())<div class="am-alert am-alert-danger" data-am-alert><p>{{ $errors->first() }}</p></div>@endif{{ Form::model($user, array('url' => 'user/' . $user->id, 'method' => 'PUT', 'class' => 'am-form')) }}{{ Form::label('email', 'E-mail:') }}<input id="email" name="email" type="email" readonly="readonly" value="{{ $user->email }}"/><br/>{{ Form::label('nickname', 'NickName:') }}<input id="nickname" name="nickname" type="text" value="{{{ $user->nickname }}}"/><br/>{{ Form::label('old_password', 'OldPassword:') }}{{ Form::password('old_password') }}<br/>{{ Form::label('password', 'NewPassword:') }}{{ Form::password('password') }}<br/>{{ Form::label('password_confirmation', 'ConfirmPassword:') }}{{ Form::password('password_confirmation') }}<br/><div class="am-cf">{{ Form::submit('Modify', array('class' => 'am-btn am-btn-primary am-btn-sm am-fl')) }}</div>{{ Form::close() }}<br/></div></div> @stop

在routes.php中添加:

Route::get('user/{id}/edit', array('before' => 'auth', 'as' => 'user.edit', function($id) {if (Auth::user()->is_admin or Auth::id() == $id) {return View::make('users.edit')->with('user', User::find($id));} else {return Redirect::to('/');} }));

上面的as是命名路由,在生成URL時(shí)也可以使用別名。例如Redirect::route('user.edit', $id)。

現(xiàn)在登錄后在右上角會(huì)發(fā)現(xiàn)多了一個(gè)Information的選項(xiàng),點(diǎn)擊后會(huì)顯示用戶個(gè)人信息的頁(yè)面:

你是不是發(fā)現(xiàn)了表單中的Form::model($user),它會(huì)根據(jù)View::make('users.edit')->with('user', User::find($id))傳過(guò)來(lái)的user進(jìn)行自動(dòng)填充。

之后就要實(shí)現(xiàn)真正地修改用戶信息了,在routes.php中增加:

Route::put('user/{id}', array('before' => 'auth|csrf', function($id) {if (Auth::user()->is_admin or (Auth::id() == $id)) {$user = User::find($id);$rules = array('password' => 'required_with:old_password|min:6|confirmed','old_password' => 'min:6',);if (!(Input::get('nickname') == $user->nickname)){$rules['nickname'] = 'required|min:4||unique:users,nickname';}$validator = Validator::make(Input::all(), $rules);if ($validator->passes()){if (!(Input::get('old_password') == '')) {if (!Hash::check(Input::get('old_password'), $user->password)) {return Redirect::route('user.edit', $id)->with('user', $user)->with('message', array('type' => 'danger', 'content' => 'Old password error'));} else {$user->password = Hash::make(Input::get('password'));}}$user->nickname = Input::get('nickname');$user->save();return Redirect::route('user.edit', $id)->with('user', $user)->with('message', array('type' => 'success', 'content' => 'Modify successfully'));} else {return Redirect::route('user.edit', $id)->withInput()->with('user', $user)->withErrors($validator); }} else {return Redirect::to('/');} }));

現(xiàn)在嘗試修改信息,如果失敗,就會(huì)出現(xiàn)錯(cuò)誤提示就像下面這樣:

如果成功就會(huì)像下面這樣:

這樣修改個(gè)人信息的功能就完成了。

4.管理用戶

上面的完成之后,我們就需要管理員能夠管理用戶,例如可以修改其他用戶的昵稱、重置它們的密碼、鎖定用戶等。先需要重寫(xiě)下_layouts/nav.blade.php的@if (Auth::check())里的內(nèi)容:

@if (Auth::user()->is_admin) <ul class="am-nav am-nav-pills am-topbar-nav"><li class=""><a href="#">Users</a></li> </ul> @endif <div class="am-topbar-right"><div class="am-dropdown" data-am-dropdown="{boundary: '.am-topbar'}"><button class="am-btn am-btn-secondary am-topbar-btn am-btn-sm am-dropdown-toggle" data-am-dropdown-toggle><span class="am-icon-users"></span> {{{ Auth::user()->nickname }}} <span class="am-icon-caret-down"></span></button><ul class="am-dropdown-content"><li><a href="{{ URL::to('user/'. Auth::id() . '/edit') }}"><span class="am-icon-user"></span> Information</a></li><li><a href="{{ URL::to('logout') }}"><span class="am-icon-power-off"></span> Exit</a></li></ul></div> </div>

創(chuàng)建用戶列表視圖:

$ php artisan generate:view admin.users.list

修改views/admin/users/list.blade.php:

@extends('_layouts.default')@section('main') <div class="am-g am-g-fixed"><div class="am-u-sm-12"><br/>@if (Session::has('message'))<div class="am-alert am-alert-{{ Session::get('message')['type'] }}" data-am-alert><p>{{ Session::get('message')['content'] }}</p></div>@endif<table class="am-table am-table-hover am-table-striped "><thead><tr><th>ID</th><th>E-mail</th><th>Nickname</th><th>Management</th></tr></thead><tbody>@foreach ($users as $user)<tr><td>{{ $user->id }}</td><td>{{ $user->email }}</td><td>{{{ $user->nickname }}}</td><td><a href="{{ URL::to('user/'. $user->id . '/edit') }}" class="am-btn am-btn-xs am-btn-primary">Edit</a>{{ Form::open(array('url' => 'user/' . $user->id . '/reset', 'method' => 'PUT', 'style' => 'display: inline;')) }}<button type="button" class="am-btn am-btn-xs am-btn-warning" id="reset{{ $user->id }}">Reset</button>{{ Form::close() }}@if ($user->block){{ Form::open(array('url' => 'user/' . $user->id . '/unblock', 'method' => 'PUT', 'style' => 'display: inline;')) }}<button type="button" class="am-btn am-btn-xs am-btn-danger" id="unblock{{ $user->id }}">Unblock</button>{{ Form::close() }}@else{{ Form::open(array('url' => 'user/' . $user->id, 'method' => 'DELETE', 'style' => 'display: inline;')) }}<button type="button" class="am-btn am-btn-xs am-btn-danger" id="delete{{ $user->id }}">Block</button>{{ Form::close() }}@endif</td></tr>@endforeach</tbody></table></div> </div><div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm"><div class="am-modal-dialog"><div class="am-modal-bd"></div><div class="am-modal-footer"><span class="am-modal-btn" data-am-modal-cancel>No</span><span class="am-modal-btn" data-am-modal-confirm>Yes</span></div></div> </div> <script>$(function() {$('[id^=reset]').on('click', function() {$('.am-modal-bd').text('Sure you want to reset the password for 123456?');$('#my-confirm').modal({relatedTarget: this,onConfirm: function(options) {$(this.relatedTarget).parent().submit();},onCancel: function() {}});});$('[id^=delete]').on('click', function() {$('.am-modal-bd').text('Sure you want to lock it?');$('#my-confirm').modal({relatedTarget: this,onConfirm: function(options) {$(this.relatedTarget).parent().submit();},onCancel: function() {}});});$('[id^=unblock]').on('click', function() {$('.am-modal-bd').text('Sure you want to unlock it?');$('#my-confirm').modal({relatedTarget: this,onConfirm: function(options) {$(this.relatedTarget).parent().submit();},onCancel: function() {}});});}); </script> @stop

上面的@foreach相當(dāng)于for循環(huán),可以遍歷@users中的內(nèi)容。

把views/_layouts/default.blade.php中

<script src="//cdn.bootcss.com/jquery/2.1.3/jquery.min.js"></script> <script src="//cdn.amazeui.org/amazeui/2.1.0/js/amazeui.min.js"></script>

移到head中。

為了保證只有管理員才能管理用戶,我們?cè)赼pp/filters.php中增加一個(gè)過(guò)濾器:

Route::filter('idAdmin', function() {if (!Auth::user()->is_admin) {return Redirect::to('/');} });

在routes.php中增加:

Route::group(array('prefix' => 'admin', 'before' => 'auth|isAdmin'), function() {Route::get('users', function(){return View::make('admin.users.list')->with('users', User::all())->with('page', 'users');}); });Route::model('user', 'User');Route::group(array('before' => 'auth|csrf|isAdmin'), function() {Route::put('user/{user}/reset', function(User $user){$user->password = Hash::make('123456');$user->save();return Redirect::to('admin/users')->with('message', array('type' => 'success', 'content' => 'Reset password successfully'));});Route::delete('user/{user}', function(User $user){$user->block = 1;$user->save();return Redirect::to('admin/users')->with('message', array('type' => 'success', 'content' => 'Lock user successfully'));});Route::put('user/{user}/unblock', function(User $user){$user->block = 0;$user->save();return Redirect::to('admin/users')->with('message', array('type' => 'success', 'content' => 'Unlock user successfully'));}); });

上面使用了路由組Route::group、路由前綴prefix和路由與模型綁定Route::model,過(guò)濾器是可以有多個(gè)的用|分隔。

現(xiàn)在用管理員賬號(hào)登錄后會(huì)發(fā)現(xiàn)導(dǎo)航條多了一個(gè)Users鏈接,點(diǎn)擊Users超鏈接會(huì)出現(xiàn)下圖這樣:

上面的用戶數(shù)據(jù)需要自己添加,當(dāng)點(diǎn)擊Block的是否會(huì)出現(xiàn)確認(rèn)的提示框:

點(diǎn)擊Yes操作成功后會(huì)像下面這樣:

5.小結(jié)

這節(jié)完成了用戶管理模塊,但是還有很多不完善的地方,你可以在用戶列表頁(yè)面添加按昵稱或Email查找用戶、只顯示鎖定的用戶等功能,還有你是不是發(fā)現(xiàn)了在routes.php中代碼顯得很零亂,那是因?yàn)槲覀冞€沒(méi)有使用MVC模式中的C,在下節(jié)教程中就將講解Laravel中的控制器。


  • Laravel官網(wǎng)
  • 中文文檔1、中文文檔2
  • 實(shí)驗(yàn)樓論壇
  • Laravel中文網(wǎng)問(wèn)答社區(qū)
  • PHPHub中文社區(qū)
  • API文檔
  • laravel.io
  • LARACASTS


總結(jié)

以上是生活随笔為你收集整理的Laravel大型项目系列教程(二)之用户管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。