如何解决Slim Framework - PHP 没有正确路由 - 有什么想法吗?
我正在使用 XAMPP 运行 Web 服务器 - 它以“http://localhost/mvc-framework/public/”为根运行。
但是当我尝试使用“联系人”链接进行导航时,它会转到“http://localhost/contact”并且我收到“404”错误。
我的 routes.php
目前看起来像这样:
<?php
// Routes
$app->get('mvc-framework/public/contact',function ($request,$response,$args) {
// Render index view
return $this->view->render($response,'contact-form.phtml',$args);
});
$app->get('/[{name}]',$args) {
// Sample log message
$this->logger->info("Slim-Skeleton '/' route");
// Render index view
return $this->view->render($response,'about.phtml',$args);
});
有什么地方明显我出错了吗?
解决方法
public/
目录只是网络服务器的 DocumentRoot,因此不应成为 url 基本路径的一部分。
正确的网址:http://localhost/mvc-framework/
当您在 DocumentRoot 的子目录中运行您的 Slim 应用程序时,您还必须定义 Slim basePath。在您的特定情况下,它是 /mvc-framework
:
$app->setBasePath('/mvc-framework');
应该在没有基本路径的情况下定义路由:
<?php
$app->get('/contact',function ($request,$response,$args) {
// Render index view
return $this->view->render($response,'contact-form.phtml',$args);
});
当您在 DocumentRoot 的子目录中运行 Slim 应用程序时,还要确保添加正确的 .htaccess
文件。 Slim 4 BasePath
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。