parseRoutesNoCheck 在 Yesod 中的位置

发布于 2024-12-08 06:28:08 字数 3044 浏览 0 评论 0原文

我开始学习Haskell语言和Yesod Web框架。 然而,当我尝试对 mkYesod 使用“parseRoutesNoCheck”时,编译器无法匹配 parseRoutesNoCheck 的返回类型(资源)。

$ ghc simple_yesod.hs
[1 of 1] Compiling Main             ( simple_yesod.hs, simple_yesod.o )
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package array-0.3.0.2 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package deepseq-1.1.0.2 ... linking ... done.
Loading package text-0.11.0.6 ... linking ... done.
Loading package path-pieces-0.0.0 ... linking ... done.
Loading package pretty-1.0.1.2 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package web-routes-quasi-0.7.1 ... linking ... done.

simple_yesod.hs:9:36:
    Couldn't match expected type `yesod-core-0.9.2:Yesod.Internal.RouteParsing.Resource'
                with actual type `Resource'
    In the return type of a call of `Resource'
    In the expression:
      Resource "PageR" [StaticPiece "page", SinglePiece "String"] ["GET"]
    In the second argument of `mkYesod', namely
      `[Resource
          "PageR" [StaticPiece "page", SinglePiece "String"] ["GET"],
        Resource
          "UserR" [StaticPiece "user", SinglePiece "String"] ["GET"]]'

看来我使用了错误的 parseRoutesNoCheck,但是正确的模块在哪里?

simple_yesod.hs 如下。

{-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-}
import Yesod
import Web.Routes.Quasi.Parse
import qualified Text.Blaze.Html5 as H

data Test = Test {
  }

mkYesod "Test" [parseRoutesNoCheck|
/page/#String      PageR   GET
/user/#String      UserR   GET
|]

instance Yesod Test where
  approot _ = "" 
  defaultLayout widget = do
                      content <- widgetToPageContent widget
                      hamletToRepHtml [hamlet|
\<!DOCTYPE html>

<html>
  <head>
    <title>#{pageTitle content}
  <body>
    <ul id="navbar">
    <div id="content">
      \^{pageBody content}
|]


getUserR :: String -> Handler RepHtml
getUserR userName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Hello " ++ userName
                      addHamlet $ html userName
                    )
    where
      html page = [hamlet|
 <h1>User: #{userName}
 <p>This page is for user: #{userName}
 |]

getPageR :: String -> Handler RepHtml
getPageR pageName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Article: " ++ pageName
                      addHamlet $ html pageName
                    )
    where
      html page = [hamlet|
 <h1>Page: #{pageName}
 <p>This page is for page: #{pageName}
 |]

main :: IO ()
main = do
  warpDebug 3000 $ Test

我正在使用 Glasgow Haskell 编译器,版本 7.0.3 和 yesod-core-0.9.2。

I started to learn Haskell language and Yesod web framework.
When I tried to use "parseRoutesNoCheck" for mkYesod, however, the compiler could not match the return type (Resource) of parseRoutesNoCheck.

$ ghc simple_yesod.hs
[1 of 1] Compiling Main             ( simple_yesod.hs, simple_yesod.o )
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package array-0.3.0.2 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package deepseq-1.1.0.2 ... linking ... done.
Loading package text-0.11.0.6 ... linking ... done.
Loading package path-pieces-0.0.0 ... linking ... done.
Loading package pretty-1.0.1.2 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package web-routes-quasi-0.7.1 ... linking ... done.

simple_yesod.hs:9:36:
    Couldn't match expected type `yesod-core-0.9.2:Yesod.Internal.RouteParsing.Resource'
                with actual type `Resource'
    In the return type of a call of `Resource'
    In the expression:
      Resource "PageR" [StaticPiece "page", SinglePiece "String"] ["GET"]
    In the second argument of `mkYesod', namely
      `[Resource
          "PageR" [StaticPiece "page", SinglePiece "String"] ["GET"],
        Resource
          "UserR" [StaticPiece "user", SinglePiece "String"] ["GET"]]'

It seems that I'm using wrong parseRoutesNoCheck, but where is the correct Module?

simple_yesod.hs is below.

{-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-}
import Yesod
import Web.Routes.Quasi.Parse
import qualified Text.Blaze.Html5 as H

data Test = Test {
  }

mkYesod "Test" [parseRoutesNoCheck|
/page/#String      PageR   GET
/user/#String      UserR   GET
|]

instance Yesod Test where
  approot _ = "" 
  defaultLayout widget = do
                      content <- widgetToPageContent widget
                      hamletToRepHtml [hamlet|
\<!DOCTYPE html>

<html>
  <head>
    <title>#{pageTitle content}
  <body>
    <ul id="navbar">
    <div id="content">
      \^{pageBody content}
|]


getUserR :: String -> Handler RepHtml
getUserR userName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Hello " ++ userName
                      addHamlet $ html userName
                    )
    where
      html page = [hamlet|
 <h1>User: #{userName}
 <p>This page is for user: #{userName}
 |]

getPageR :: String -> Handler RepHtml
getPageR pageName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Article: " ++ pageName
                      addHamlet $ html pageName
                    )
    where
      html page = [hamlet|
 <h1>Page: #{pageName}
 <p>This page is for page: #{pageName}
 |]

main :: IO ()
main = do
  warpDebug 3000 $ Test

I'm using Glasgow Haskell Compiler, Version 7.0.3 and yesod-core-0.9.2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-12-15 06:28:08

您应该简单地使用 parseRoutes 而不是 parseRoutesNoCheck。另外,您可能需要添加一个 module Main where 并删除 import Web.Routes.Quasi.Parse 因为 Yesod 模块已经导出 解析路由

这是经过我提到的修改的完整代码。

{-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-}
module Main where
import Yesod
import qualified Text.Blaze.Html5 as H


data Test = Test {
  }

mkYesod "Test" [parseRoutes|
/page/#String      PageR   GET
/user/#String      UserR   GET
|]

instance Yesod Test where
  approot _ = "" 
  defaultLayout widget = do
                      content <- widgetToPageContent widget
                      hamletToRepHtml [hamlet|
\<!DOCTYPE html>

<html>
  <head>
    <title>#{pageTitle content}
  <body>
    <ul id="navbar">
    <div id="content">
      \^{pageBody content}
|]


getUserR :: String -> Handler RepHtml
getUserR userName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Hello " ++ userName
                      addHamlet $ html userName
                    )
    where
      html page = [hamlet|
 <h1>User: #{userName}
 <p>This page is for user: #{userName}
 |]

getPageR :: String -> Handler RepHtml
getPageR pageName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Article: " ++ pageName
                      addHamlet $ html pageName
                    )
    where
      html page = [hamlet|
 <h1>Page: #{pageName}

一个好主意是当您处于 Yesod 的学习阶段(以及与此相关的其他所有内容)时,尝试复制现有示例。代码片段通常可以在 Yesod 书或 github 存储库中找到,可以从这些来源中学习。


编辑:我缺乏完整的答案。显然,现在 parseRoutes 及其系列位于 "Yesod.Dispatch" 仅从隐藏模块重新导出 Yesod.Internal .RouteParsingparseRoutesNoCheck 在 Yesod.Internal.RouteParsing 中定义,但从未公开,因为您可能总是希望检查非重叠路由。

我希望这能澄清一点。

You should simply use parseRoutes rather than parseRoutesNoCheck. Also you might want to add a module Main where and remove import Web.Routes.Quasi.Parse since the Yesod module already exports parseRoutes.

Here is the complete code with the modifications I mentioned.

{-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-}
module Main where
import Yesod
import qualified Text.Blaze.Html5 as H


data Test = Test {
  }

mkYesod "Test" [parseRoutes|
/page/#String      PageR   GET
/user/#String      UserR   GET
|]

instance Yesod Test where
  approot _ = "" 
  defaultLayout widget = do
                      content <- widgetToPageContent widget
                      hamletToRepHtml [hamlet|
\<!DOCTYPE html>

<html>
  <head>
    <title>#{pageTitle content}
  <body>
    <ul id="navbar">
    <div id="content">
      \^{pageBody content}
|]


getUserR :: String -> Handler RepHtml
getUserR userName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Hello " ++ userName
                      addHamlet $ html userName
                    )
    where
      html page = [hamlet|
 <h1>User: #{userName}
 <p>This page is for user: #{userName}
 |]

getPageR :: String -> Handler RepHtml
getPageR pageName = defaultLayout
                    (do
                      setTitle $ H.toHtml $ "Article: " ++ pageName
                      addHamlet $ html pageName
                    )
    where
      html page = [hamlet|
 <h1>Page: #{pageName}

A good idea is to try to copy existing examples when you're in the learning phase of Yesod (and everything else for that matter). Code snippets are often found in the Yesod book or in github repos, one can learn from those sources.


Edit: I lack a complete answer. Apperently nowadays the parseRoutes and family lies in "Yesod.Dispatch" which only is reexporting from the hidden module Yesod.Internal.RouteParsing. parseRoutesNoCheck is defined in Yesod.Internal.RouteParsing but never exposed, since you probably always want checking of non-overlapping routes.

I hope this clarifies a bit more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文