Typecho——Argument 1 passed to TypechoRouter::get() must be of the type string

2024-08-15 14:18:29 浏览数 (5)

前言

开发EasyBe主题的置顶功能时候,测试的时候出现了TypechoRouter::get()报错,根据对应的报错信息查看了下源码后解决了该问题;

内容

查询SQL

代码语言:javascript复制
// 获取全部文章
function getAllPosts($page, $limit) {
    $db = Typecho_Db::get();
    $sql = $db->select('c.cid', 'c.title', 'c.created', 'c.text', 'c.password', 'c.commentsNum', 'c.views', 'c.digg', 'f.str_value as sticky')
            ->from('table.contents as c')
            ->join('table.fields as f', 'f.cid = c.cid', Typecho_Db::LEFT_JOIN)
            ->where('c.status = ? and c.type = ?', 'publish', 'post')
            ->order('sticky', Typecho_Db::SORT_DESC)
            ->order('c.created', Typecho_Db::SORT_DESC)
            ->page($page, $limit);
    return  $db->fetchAll($sql);
}

报错信息

根据报错,这里我们只需要关注#0的提示即可;

代码语言:javascript复制
Argument 1 passed to TypechoRouter::get() must be of the type string, null given, called in /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php on line 536
TypeError: Argument 1 passed to TypechoRouter::get() must be of the type string, null given, called in /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php on line 536 and defined in /data/wwwroot/dev.wangyangyang.vip/build/var/Typecho/Router.php:170
Stack trace:
#0 /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php(536): TypechoRouter::get(NULL)
#1 /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php(478): WidgetBaseContents->filter(Array)
#2 /data/wwwroot/dev.wangyangyang.vip/build/usr/themes/easybe/index.php(19): WidgetBaseContents->push(Array)
#3 /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Archive.php(1415): require_once('/data/wwwroot/d...')
#4 /data/wwwroot/dev.wangyangyang.vip/build/var/Typecho/Router.php(99): WidgetArchive->render()
#5 /data/wwwroot/dev.wangyangyang.vip/build/index.php(23): TypechoRouter::dispatch()
#6 {main}

排查经过

根据报错进入到对应的文件中,使用vi进行编辑;

?> 到这里其实问题已经就解决了,因为我上面查询的SQL中,并没有将字段type筛选出来,所以这里才导致了传NULL的情况;

解决

把~c c.type c~加入查询字段即可;

代码语言:javascript复制
// 获取全部文章
function getAllPosts($page, $limit) {
    $db = Typecho_Db::get();
    $sql = $db->select('c.cid', 'c.title', 'c.created', 'c.text', 'c.password', 'c.commentsNum', 'c.views', 'c.type', 'c.digg', 'f.str_value as sticky')
            ->from('table.contents as c')
            ->join('table.fields as f', 'f.cid = c.cid', Typecho_Db::LEFT_JOIN)
            ->where('c.status = ? and c.type = ?', 'publish', 'post')
            ->order('sticky', Typecho_Db::SORT_DESC)
            ->order('c.created', Typecho_Db::SORT_DESC)
            ->page($page, $limit);
    return  $db->fetchAll($sql);
}

1 人点赞