上一教程:http://blog.csdn.net/small_rice_/article/details/45269659
#创建用户注册视图:
php artisan generate:view users.create
@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
@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'); });访问:localhost:8000/register
#2、实现用户注册
# 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); } }));unique:users,email 确保users的email 字段唯一、users和email 不能有空格、confirmed 确保password_conformation 和password 的值一致
#routes.php 的post login 内的返回 消息
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
测试注册
#3、修改个人信息
#在_layouts/nav.blade.php 添加修改个人信息选项(exit 上面)
<li><a href="{{ URL::to('user/'. Auth::id() . '/edit') }}"><span class="am-icon-user"></span> Information</a></li>
#添加编辑视图
php artisan generate:view users.edit
#修改模板
@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> @stopForm::model($user) 根据下面路由 View::make('users.edit')->with('user', User::find($id)); 传来的user 进行填充
#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 是命名路由
#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('/'); } }));
#修改导航模板 _layouts/nav.blade.php 里面 @if(Auth::check()) 内容替换为
@if (Auth::user()->is_admin) <ul class="am-nav am-nav-pills am-topbar-nav"> <li class="{{ (isset($page) and ($page == 'users')) ? 'am-active' : '' }}"><a href="{{ URL::to('admin/users') }}">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>
php artisan generate:view admin.users.list
@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 遍历
#上移views/_layouts/default.blade.php
<script src="http://labfile.oss.aliyuncs.com/jquery/2.1.3/jquery.min.js"></script> <script src="http://labfile.oss.aliyuncs.com/amazeui/2.2.1/js/amazeui.min.js"></script>
到head 中
#app/filters.php 添加过滤器只有管理员才能管理用户的过滤器
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 过滤器多个由 | 分割
这节完成了用户管理模块,但是还有很多不完善的地方,你可以在用户列表页面添加按昵称或Email查找用户、只显示锁定的用户等功能,还有你是不是发现了在routes.php中代码显得很零乱,那是因为我们还没有使用MVC模式中的C,在下节教程中就将讲解Laravel中的控制器。这节完成了用户管理模块,但是还有很多不完善的地方,你可以在用户列表页面添加按昵称或Email查找用户、只显示锁定的用户等功能,还有你是不是发现了在routes.php中代码显得很零乱,那是因为我们还没有使用MVC模式中的C,在下节教程中就将讲解Laravel中的控制器。
参照:https://www.shiyanlou.com/courses/document/406