访问.phtml模板路由时,Slim PHP framework 3.0:404错误

Server configuration:

  • Ubuntu 18.04
  • Apache 2.4.29
  • PostgreSQL 10.6
  • PHP 7.3 (I was using 7.2 but the system has decided to update it for me, I will rollback once I have resolved the current bugs)
  • Composer
  • Slim/Slim 3.0
  • Slim/php-view

I have been building an API with the Slim PHP framework, using gothinkster's repository as a starting point. The code has been heavily adapted, though all code relating to rendering templates remains intact. When I access the website's homepage, the index.phtml file is rendered properly, but following any hyperlinks from th homepage results in a 404 error, although the .phtml files all reside within the same directory.

The following are excerpts of my code, excluding irrelevant configuration. Although I have only displayed one endpoint, the API defines 10 endpoints, all of which are get requests with no arguments, using .phtml files which reside within the same folder.

Settings, routes and dependencies are called using require statements within the index.php file.

/[api_root]/public/index.php:

if (PHP_SAPI == 'cli-server') {

    $url  = parse_url($_SERVER['REQUEST_URI']);

    $file = __DIR__ . $url['path'];

    if (is_file($file)) {

        return false;

    }
}

require __DIR__ . '/../vendor/autoload.php';

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';

$app = new \Slim\App($settings);

// Set up dependencies
require __DIR__ . '/../src/dependencies.php';

// Register middleware
require __DIR__ . '/../src/middleware.php';

// Register routes
require __DIR__ . '/../src/routes.php';

// Run app
$app->run();

The settings file returns an array containing a reference to the template path.

[api_root]/src/settings.php:

'settings' => [
    'renderer' => [
        'template_path' => __DIR__ . '/../templates/',
    ]
]

Templates are assigned to the renderer within the dependencies.php file.

[api_root]/src/dependencies.php:

$container = $app->getContainer();

// view renderer
$container['renderer'] = function ($c) {
    $settings = $c->get('settings')['renderer'];
    return new Slim\Views\PhpRenderer($settings['template_path']);
};

.phtml templates are rendered within get requests.

[api_root]/src/routes.php

$app->get('/getting-started',
    function (Request $request, Response $response) {
        return $this->renderer->render($response, 'getting-started.phtml');
    });

The getting-started.phtml file is located within [api_root]/templates/.

The root / is defined and successfully returns the index.phtml file located within the templates directory, but when I attempt to follow any links from the homepage, I recieve a 404 error. The anchor which points to the getting-started page successfully redirects to mywebsiteurl.com/getting-started, but the template file is not rendered and the browser responds with a 404 error.

All application files are stored privately on the server. I have created symlinks from the contents of the [api_root]/public folder to my website's public_html folder.

There is obviously something wrong with my definition of the path to the /templates directory, but I can't work out how to rectify it.

EDIT:

When I created the symlinks to the public_html folder, I had forgotten to include hidden files (namely, the .htaccess file). I have since created this symlink, though now when I attempt to access the homepage I see the default apache2 page which is displayed after apache is installed. I will note here that the website resides on a paid server. Apache is configured to use virtual hosts and I have edited the hosts file on my local machine properly. As stated, without an .htaccess file I can view the homepage, but other routes are not being found. When the .htaccess file exists in the public_html folder, I cannot view the homepage or any other routes.

.htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
  RewriteCond %{HTTP_HOST} ^(www\.)?mywebsiteurl.com
  RewriteRule ^(.*) - [E=BASE:%1]

  # If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
  # absolute physical path to the directory that contains this htaccess file.
  # RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>
dongmi5607
dongmi5607 感谢您指出了这一点。我已经更新了我的答案。正如我在编辑中解释的那样,我实际上忘了为.htaccess文件创建一个符号链接,但是自添加它以来我甚至无法访问主页,我被重定向到默认的apache页面。
接近 2 年之前 回复
douna2917
douna2917 404错误并不意味着您的应用程序找不到getting-started.phtml文件。这可能与.htaccess文件有关。您能否将该文件的内容添加到您的问题中?
接近 2 年之前 回复

1个回答



这里的问题是我没有在服务器上启用 mod_rewrite </ code>,所以我的 .htaccess </ code>文件未被阅读。</ p>
</ div>

展开原文

原文

The issue here was that I had not enabled mod_rewrite on the server, so my .htaccess file was not being read.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问