You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Yii2基础版调用HelloController的actionUserPosts出现404错误求助

问题出在URL参数格式上!

你调用actionUserPosts时用的URL里出现了两个问号(?),这既不符合HTTP GET参数的规范,也让Yii没法正确解析你的路由和参数。

先看你错误的URL:

http://localhost/hello-yii/web/index.php?r=hello/user-posts?user_id=10&bulan=2&tahun=2017

HTTP协议里,URL的参数部分只有第一个参数用?开头,后面所有的参数都得用&来分隔。你这里第二个?直接把Yii搞懵了——它会把hello/user-posts?user_id=10当成整个路由名称,自然找不到对应的动作,返回404错误。

改成这个正确的URL就可以了:

http://localhost/hello-yii/web/index.php?r=hello/user-posts&user_id=10&bulan=2&tahun=2017

把第二个?换成&,Yii就能正确识别路由是hello/user-posts,并解析后面的三个必填参数啦。

额外提醒两点:

  • 你的actionUserPosts方法要求三个参数$user_id, $bulan, $tahun都是必填的,要是URL里少传任何一个,还是会返回404,记得都带上哦。
  • 如果你想让URL更美观,可以配置Yii的URL管理器(在config/web.php的components里设置urlManager),开启美化后就能用类似http://localhost/hello-yii/web/hello/user-posts/10/2/2017这样的地址访问,对应的路由规则可以这么写:
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            'hello/user-posts/<user_id:\d+>/<bulan:\d+>/<tahun:\d+>' => 'hello/user-posts',
        ],
    ],
    

内容的提问来源于stack exchange,提问作者Misdan Wijaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:59:40