没有不值得去解决的问题,也没有不值得去学习的技术!

Yii getAlias Generates Different Image URLs for /case and /case/, Causing 404

When the URL is http://official.local/case/, the generated  results in an image URL of http://official.local/case/images/top.png, returning a 404 response. How can this be resolved?

1. The code implementation in the view is as follows

When the URL is http://official.local/case, the generated results in an image URL of http://official.local/static/images/header/top.png, returning a 200 response.

When the URL is http://official.local/case/, the generated results in an image URL of http://official.local/case/images/top.png, returning a 404 response. How can this be resolved? See Figure 1.

When the URL is http://official.local/case/, the generated <img class="top-icon" src="./images/top.png"> results in an image URL of http://official.local/case/images/top.png, returning a 404 response. How can this be resolved?
Figure 1

2. Viewing the page source code of http://official.local/case/, the generated results in an image URL of http://official.local/case/images/top.png, returning a 404 response. See Figure 2.

Viewing the page source code of http://official.local/case/, the generated <img class="top-icon" src="./images/top.png"> results in an image URL of http://official.local/case/images/top.png, returning a 404 response.
Figure 2

3. Checking the Debug information, an error had already occurred, returning a 404 response: yii\base\InvalidRouteException: Unable to resolve the request “case/”. See Figure 3.

Checking the Debug information, an error had already occurred, returning a 404 response: yii\base\InvalidRouteException: Unable to resolve the request "case/".
Figure 3
Plaintext
yii\base\InvalidRouteException: Unable to resolve the request "case/". in C:\wwwroot\official-website-management-system\src\vendor\yiisoft\yii2\base\Module.php:561
Stack trace:
#0 C:\wwwroot\official-website-management-system\src\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('case/', Array)
#1 C:\wwwroot\official-website-management-system\src\vendor\yiisoft\yii2\base\Application.php(384): yii\web\Application->handleRequest(Object(yii\web\Request))
#2 C:\wwwroot\official-website-management-system\src\frontend\web\index.php(18): yii\base\Application->run()
#3 {main}

Next yii\web\NotFoundHttpException: Page not found. in C:\wwwroot\official-website-management-system\src\vendor\yiisoft\yii2\web\Application.php:115
Stack trace:
#0 C:\wwwroot\official-website-management-system\src\vendor\yiisoft\yii2\base\Application.php(384): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 C:\wwwroot\official-website-management-system\src\frontend\web\index.php(18): yii\base\Application->run()
#2 {main}

4. Ultimately, I implemented a small helper class method.

PHP
    public static function url($path): string
    {
        $cdn = Yii::$app->params['cdnDomainHttps'] ?? '';
        return rtrim($cdn, '/') . '/' . ltrim($path, '/');
    }



关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系