要读取经过身份验证的用户,您可以编写:
getUserProfileR :: Handler RepHtml
getUserProfileR = do
userId <- requireAuthId
Entity _ userData <- runDB $ selectFirst [UserProfileUser ==. userId] [] >>= return.fromJust
defaultLayout $ do
setTitle "User profile"
$(widgetFile "userprofile")
列出所有,删除过滤器,select
改用selectFirst
并写(在小部件上)一些像
<h1>User list</h1>
<table>
<tr>
<th>Name
<th>Mail
<th>...
$forall (Entity _ userData) <- userList
<tr>
<td>#{userProfileName userData}
<td>#{userProfileMail userData}
<td>...
(对不起,我没有写一个完整且经过测试的解决方案,但我这里没有开发沙箱)
编辑
一个完整且经过测试的示例。(是否需要 yesod-platform 和 yesod-bin)
$ ghc-pkg list | grep yesod-[0-9]
yesod-1.2.2.1
$ yesod init
$ cd userList
为简单起见,编辑“Handler/Home.hs”文件并添加
getUserListR :: Handler Html
getUserListR = do
users <- runDB $ selectList [] []
defaultLayout $ do
setTitle "Public user list!"
[whamlet|
<h1>User list</h1>
<table>
<tr>
<th>Mail
$forall (Entity _ userData) <- users
<tr>
<td>#{userIdent userData}
|]
将下一行添加到“config/routes”中
/userlist UserListR GET
去测试
$ cabal install
$ yesod devel
输入一些用户登录和注销
http://site:port/auth/login
(do login)
http://site:port/auth/logout
列出用户
http://site:port/userlist
;)