dourong4031 2018-11-24 08:45
浏览 158
已采纳

访问.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>
  • 写回答

1条回答 默认 最新

  • douyimiao1993 2018-11-29 07:09
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿