Slim 2.6.2 render() 仅适用于 Index.html

Slim 2.6.2 render() Only Works for Index.html

我目前正在 TeamTreehouse.com 学习 Slim 框架,运行 遇到一个我无法解决的问题。

在项目的这一点上,我们已经通过 Composer 安装了 Slim,并在我们的文档根目录(在我的计算机上是 /home/daniel/src/public_html/treehouse/build_websites_php/)中设置了 .htaccess 和 index.php 文件。 在模板文件夹中,我们有 index.html 和 contact.html。这是文件夹的布局。

在index.php中,我实例化了一个新的Slim对象:

$app = new \Slim\Slim();

然后在url为localhost/treehouse/build_websites/和localhost/treehouse/build_websites/联系人,分别为。

$app->get('/', function () use($app) {
    $app->render('index.html');
});

$app->get('/contact', function () use($app) {
    $app->render('contact.html');
});

然后 运行 应用:

$app->run();

我的 index.html 页面显示正常,但是当我尝试访问 /contact url 时出现 404 错误(不是通过 Slim,只是服务器的默认错误)。以下是我的系统的一些规格:

我的 /home/daniel/src/public_html/ 目录中的任何内容都可以通过 Apache 访问,因为我在过去的一年里从那里 运行 PHP 脚本。

我已经尝试了 here 的建议(并在每次更新到 conf.d 或其他文件后重新启动服务器)但没有成功。

任何帮助将不胜感激,我只使用 PHP/Ubuntu/Apache 大约一年,所以我可能遗漏了一些明显的东西!

这里是 index.php 文件:

<?php

require 'vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/', function () use($app) {
  /* When using render(), the url localhost/treehouse/build_websites_php/ to 
  gets you the home page. */
  $app->render('index.html');
});

/* This SHOULD bring up the contact page at url 
localhost/treehouse/build_websites_php/contact, but it doesn't! */
$app->get('/contact', function () use($app) {
  $app->render('contact.html');
});

$app->run();

?>

这里是 .htacess 文件:

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /home/daniel/src/public_html/treehouse/build_websites_php/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

下面是 Apache 的各种 conf.d 文件:

/etc/apache2/apache2.conf

# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /home/daniel/src/public_html>
    Order allow,deny    
    Allow from all
    Require all granted
</Directory>

我尝试按照建议 here 将 AllowOverride All 添加到最后一条指令,然后我根本无法从服务器访问 PHP 文件,并收到 500 错误而不是 404错误。

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
     DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/etc/apache2/sites-available/mysite.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /home/daniel/src/public_html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

您的 Apache 设置似乎不允许 .htaccess 文件覆盖任何设置。在 apache2.conf.

中添加如下内容
<Directory /home/daniel/src/public_html>
    AllowOverride All
    Order allow,deny    
    Allow from all
    Require all granted
</Directory>

好的哇,感谢 Mika 为我指明了正确的方向,我终于弄明白了。事实证明,我的 /etc/apache2/sites-available/mysite.conf 文件需要以下指令:

<Directory /home/daniel/src/public_html >
    AllowOverride All
</Directory>

除了像这样的其他指令之外,我还尝试将该指令添加到 /etc/apache2/apache2.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /home/daniel/src/public_html>
    Order allow,deny    
    Allow from all
    Require all granted
    AllowOverride All #THIS DIDN'T WORK
</Directory>

但是上面的 AllowOverride All 通过来自服务器的 500 错误。显然,它必须单独存在于 /etc/apache2/sites-available/mysite.conf 文件中,谁知道呢!

我还 运行 sudo a2enmod rewrite && sudo /etc/init.d/apache2 restart 以确保 mod_rewrite 在发现此错误消息后已加载(感谢 Mika 指出检查日志文件!):

[Sun Nov 13 10:37:51.054347 2016] [core:alert] [pid 10979] [client ::1:51900] /home/daniel/src/public_html /treehouse/build_websites_php/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

我在添加 AllowOverride All 指令之前这样做了,所以我不确定它是否在解决问题中发挥了作用,但我想我会记录它以供感兴趣的人使用。

这些站点提供了有关如何最终解决问题的宝贵信息: