# 管理用戶
本節與下一節聯動,通過添加管理員權限然后對所有用戶進行查看,刪除等操作。
## 列出用戶
非常簡單的,我們只需要在控制器里使用模型返回所有值即可。
`application\user\controller\Auth.php`
~~~php
public function index()
{
return User::all();
}
~~~
打開瀏覽器訪問 http://thinkphp.test/user/auth/index ,卻直接跳轉到“非法操作”的提示,原因在于我們上一節創建用戶一致性策略時,判斷了傳入 `id` 和 `session id` 的一致性,可是在 index 控制器內,我們并沒有傳入 `id`,那么則會返回 `false`。
`application\behavior\UserPolicy.php`
~~~php
public function run($params)
{
$user_id = Session::get('user.id');
if (!is_null($params)) {
// 存在傳入 ID $params
return
(int) $user_id === (int) $params
? true
: false;
} elseif (is_null($user_id)) {
// 用戶未登錄
return false;
} else {
/**
* 不存在傳入 ID 但是用戶已登錄
* 效果等同于 if(is_null($params) && !is_null($user_id))
*/
return true;
}
}
~~~
再訪問 http://thinkphp.test/user/auth/index ,即可看到從數據庫拿到的 JSON 數據。
## 創建 Web 頁面
`resources\views\user\auth\index.blade.php`
~~~html
@extends('_layout.default')
@section('title', '查看所有用戶')
@section('content')
<div class="offset-md-2 col-md-8">
<h2 class="mb-4 text-center">所有用戶</h2>
<div class="list-group list-group-flush">
@foreach ($users as $user)
<div class="list-group-item">
<a href="{{ url('user/auth/read', ['id' => $user->id]) }}">
{{ $user->name }}
</a>
</div>
@endforeach
</div>
</div>
@stop
~~~
控制器中傳入 users 數據
`application\user\controller\Auth.php`
~~~php
public function index()
{
$this->assign([
'users' => User::all()
]);
return $this->fetch();
}
~~~
訪問 http://thinkphp.test/user/auth/index 即可看到剛剛創建好的 Web 頁面。
用戶頁面中,我們可以看到只有僅僅的幾條手動創建的數據,數據太少不利于接下來進行刪除操作的測試,所以我們添加一個“批量生成用戶”的功能。
## 假數據填充
我們現在去創建一個 Users 的數據填充。
1. 鍵入命令 `php think seed:create Users`
2. 提示 `Create seeds directory? [y]/n (yes/no) [yes]:`
3. 鍵入 y 并回車
4. 提示創建成功 `created .\database\seeds\Users.php`
打開剛剛創建好的填充文件 `database\seeds\Users.php`
~~~php
<?php
use think\migration\Seeder;
class Users extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$faker = Faker\Factory::create();
$data = [];
for ($i = 0; $i < 200; $i++) {
$data[] = [
'name' => $faker->userName,
'password' => $faker->password,
'email' => $faker->email,
];
}
$this->table('users')->insert($data)->save();
}
}
~~~
以上方法中,我們使用了 `Faker\Factory` 方法,但是現在項目還未安裝此包,我們現在在命令行中鍵入
~~~bash
# 由于 Laravel-China 維護的公共鏡像過期,之前在 2.3開發環境搭建 這一節使用的鏡像地址請更換為 阿里云鏡像。鍵入命令
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
composer require fzaninotto/faker
~~~
等待 `faker` 包安裝安裝完成之后,運行數據填充命令
`php think seed:run`
再次訪問 http://thinkphp.test/user/auth/index 則會看到剛剛生成好的假數據。
## 分頁
打開控制器 `application\user\controller\Auth.php`
~~~php
public function index()
{
$this->assign([
'users' => User::paginate(10)
]);
return $this->fetch();
}
~~~
`paginate` 為分頁方法,參數 10 表示每頁提取 10 個。
再打開模板 `resources\views\user\auth\index.blade.php`
~~~html
@extends('_layout.default')
@section('title', '查看所有用戶')
@section('content')
<div class="offset-md-2 col-md-8">
<h2 class="mb-4 text-center">
所有用戶
</h2>
<div class="list-group list-group-flush">
@foreach ($users as $user)
<div class="list-group-item">
<a href="{{ url('user/auth/read', ['id' => $user->id]) }}">
{{ $user->name }}
</a>
</div>
@endforeach
</div>
<div class="mt-3">
{!! $users !!}
</div>
</div>
@stop
~~~
可以看到,我們在之前的基礎上添加了個 `{!! $users !!}`,要說明的是,`{!! !!}` 代表不通過安全過濾直接輸出。
如果我們換成 {{ $users }},則會輸出已經過濾好的 HTML 代碼
`<ul class="pagination"><li class="disabled"><sp...`
現在刷新頁面,可以看到已經能夠輸出分頁。可是每次都要通過手動輸入鏈接才能夠看到所有用戶,這樣用戶體驗不好,現在添加一個所有用戶按鈕。
`resources\views\_layout\header.blade.php`
~~~html
...
<li class="nav-item">
<a class="nav-link" href="{{ url('welcome/index/help') }}">幫助</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url('welcome/index/about') }}">關于</a>
</li>
+++
<li class="nav-item">
<a class="nav-link" href="{{ url('user/auth/index') }}">所有用戶</a>
</li>
+++
...
~~~
現在刷新頁面,頂部欄的所有用戶按鈕就顯示出來了,點擊進去也就是本節所創建好的頁面。
- 第一章. 基礎信息
- 1.1 序言
- 1.2 關于作者
- 1.3 本書源碼
- 1.4 反饋糾錯
- 1.5 安全指南
- 1.6 捐助作者
- 第二章. 開發環境布置
- 2.1 編輯器選用
- 2.2 命令行工具
- 2.3 開發環境搭建
- 2.4 瀏覽器選擇
- 2.5 第一個應用
- 2.6 Git 工作流
- 第三章. 構建頁面
- 3.1 章節說明
- 3.2 靜態頁面
- 3.3 Think 命令
- 3.4 小結
- 第四章. 優化頁面
- 4.1 章節說明
- 4.2 樣式美化
- 4.3 局部視圖
- 4.4 路由鏈接
- 4.5 用戶注冊頁面
- 4.6 集中視圖
- 4.7 小結
- 第五章. 用戶模型
- 5.1 章節說明
- 5.2 數據庫遷移
- 5.3 查看數據表
- 5.4 模型文件
- 5.5 小結
- 第六章. 用戶注冊
- 6.1 章節說明
- 6.2 注冊表單
- 6.3 用戶數據驗證
- 6.4 注冊失敗錯誤信息
- 6.5 注冊成功
- 6.6 小結
- 第七章. 會話管理
- 7.1 章節說明
- 7.2 會話
- 7.3 用戶登錄
- 7.4 退出
- 7.5 小結
- 第八章. 用戶 CRUD
- 8.1 章節說明
- 8.2 重構代碼
- 8.3 更新用戶
- 8.4 權限系統
- 8.5 列出所有用戶
- 8.6 刪除用戶
- 8.7 訪客模式
- 8.8 優化前端
- 8.9 小結
- 第九章. 微博 CRUD
- 9.1 章節說明
- 9.2 微博模型
- 9.3 顯示微博
- 9.4 發布微博
- 9.5 微博數據流
- 9.6 刪除微博
- 9.7 小結