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 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用