路径路径中的通配符星号会引发错误

时间:2021-10-02 20:06:44

A simple yesod server code with a single handler for all GET requests i wrote as:

一个简单的yesod服务器代码,为我写的所有GET请求都有一个处理程序:

{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses,
TemplateHaskell, OverloadedStrings #-}
import Yesod
data Links = Links
mkYesod "Links" [parseRoutes|
/* HomeR GET
|]
instance Yesod Links
getHomeR = defaultLayout [whamlet|Go to page 1!|]
main = warpDebug 3000 Links

It throws a cryptic error as,

它抛出一个神秘的错误,因为,

 Exception when trying to run compile-time code:
      Invalid type: ""
    Code: mkYesod
            "Links"
            ([Yesod.Routes.TH.Types.ResourceLeaf
                (Yesod.Routes.TH.Types.Resource
                   "HomeR"
                   []
                   (Yesod.Routes.TH.Types.Methods (Data.Maybe.Just []) ["GET"])
                   []
                   GHC.Types.True)])

I copied the working code from a blog and removed all but one route, added wildcard character * to the single routepath as [parseRoutes| /* HomeR GET \] to make it the handler for all urls. And, the code no longer works.

我从博客中复制了工作代码并删除了除一条路线之外的所有路径,将通配符*添加到单个路径路径中,如[parseRoutes | / * HomeR GET \]使其成为所有网址的处理程序。并且,代码不再有效。

1 个解决方案

#1


you have to add an type that is an instance of PathMultiPiece after the * like this:

你必须在*之后添加一个类型是PathMultiPiece的实例:

/*Texts HomeR GET

(Texts, which is just a synonym for [Text] is an instance because Text is an instance of PathPiece) and of course you have to add it as an argument to your handler:

(文本,它只是[Text]的同义词是一个实例,因为Text是PathPiece的一个实例),当然你必须将它作为参数添加到你的处理程序中:

getHomeR :: [Text] -> Handler Html
getHomeR parts = defaultLayout [whamlet|Go to page 1!|]

you can read more about this in the yesod documentation

您可以在yesod文档中阅读更多相关信息

#1


you have to add an type that is an instance of PathMultiPiece after the * like this:

你必须在*之后添加一个类型是PathMultiPiece的实例:

/*Texts HomeR GET

(Texts, which is just a synonym for [Text] is an instance because Text is an instance of PathPiece) and of course you have to add it as an argument to your handler:

(文本,它只是[Text]的同义词是一个实例,因为Text是PathPiece的一个实例),当然你必须将它作为参数添加到你的处理程序中:

getHomeR :: [Text] -> Handler Html
getHomeR parts = defaultLayout [whamlet|Go to page 1!|]

you can read more about this in the yesod documentation

您可以在yesod文档中阅读更多相关信息