Yesod中的查询查询参数 [英] Lookup query parameters in Yesod
问题描述
我刚刚使用 yesod init
初始化了Yesod项目(无数据库).
I just initialized a Yesod project (no database) using yesod init
.
我的 HomeR
GET处理程序如下:
My HomeR
GET handler looks like this:
getHomeR :: Handler Html
getHomeR = do
(formWidget, formEnctype) <- generateFormPost sampleForm
let submission = Nothing :: Maybe (FileInfo, Text)
handlerName = "getHomeR" :: Text
defaultLayout $ do
aDomId <- newIdent
setTitle "Welcome To Yesod!"
$(widgetFile "homepage")
使用 yesod devel
时,我可以访问 http://localhost:3000/
的默认主页.
When using yesod devel
, I can access the default homepage at http://localhost:3000/
.
在访问此URL时,如何修改上面列出的处理程序以检索(并显示)诸如 id = abc123
之类的HTTP GET查询参数:
How can I modify the handler listed above to retrieve (and display) a HTTP GET query parameter like id=abc123
when accessing this URL:
http://localhost:3000/?id = abc123
注意:该问题已通过问答式回答,因此有意不显示研究成果!
Note: This question was answered Q&A-style and therefore intentionally doesn't show research effort!
推荐答案
我将展示两种不同的方法来实现此目的.对于这两者,您都需要将此代码添加到模板中,例如在 homepage.hamlet
中:
I'll show two different methods to achieve this. For both, you'll need to add this code to your template, e.g. in homepage.hamlet
:
请注意,不能保证在访问URL时存在任何 id
参数,因此,两种方法得出的类型均为 Maybe Text
.有关模板参数的详细说明,请参见莎士比亚模板文档.
Note that it is not guaranteed there is any id
parameter present when accessing the URL, therefore the type resulting from both methods is Maybe Text
. See the Shakespearean template docs for a detailed explanation of the template parameters.
方法1:lookupGetParam
最简单的方法是使用 lookupGetParam
就像这样:
The easiest way you can do this is using lookupGetParam
like this:
idValueMaybe <- lookupGetParam "id"
使用由 yesod init
生成的默认设置时,需要在 getHomeR
和 postHomeR 中定义
idValueMaybe
code>如果模板中使用了 idValueMaybe
.
When using the default setting as generated by yesod init
, idValueMaybe
needs to be defined in both getHomeR
and postHomeR
if idValueMaybe
is used in the template.
您的 HomeR
GET处理程序可能看起来像这样:
Your HomeR
GET handler could look like this:
getHomeR :: Handler Html
getHomeR = do
idValueMaybe <- lookupGetParam "id"
(formWidget, formEnctype) <- generateFormPost sampleForm
let submission = Nothing :: Maybe (FileInfo, Text)
handlerName = "getHomeR" :: Text
defaultLayout $ do
aDomId <- newIdent
setTitle "Welcome To Yesod!"
$(widgetFile "homepage")
方法2:reqGetParams
除了按名称查找查询参数外,您还可以使用 reqGetParams
检索查询键/值对的列表.这在某些情况下可能是有利的,例如.如果您不知道所有可能的键.使用标准的 lookup
函数,您可以轻松查找该列表中的某个键.
Instead of looking up the query parameters by name, you can also retrieve a list of query key/value pairs using reqGetParams
. This can be advantageous in certain situations, e.g. if you don't know all possible keys in advance. Using the standard lookup
function you can easily lookup a certain key in that list.
代码的相关部分可能如下所示:
The relevant part of your code could look like this:
getParameters <- reqGetParams <$> getRequest
let idValueMaybe = lookup "id" getParameters :: Maybe Text
您的 getHomeR
可能看起来像这样:
Your getHomeR
could look like this:
getHomeR :: Handler Html
getHomeR = do
getParameters <- reqGetParams <$> getRequest
let idValueMaybe = lookup "id" getParameters :: Maybe Text
(formWidget, formEnctype) <- generateFormPost sampleForm
let submission = Nothing :: Maybe (FileInfo, Text)
handlerName = "getHomeR" :: Text
defaultLayout $ do
aDomId <- newIdent
setTitle "Welcome To Yesod!"
$(widgetFile "homepage")
这篇关于Yesod中的查询查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!