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

时间:2016-11-13 14:43:15

标签: php apache .htaccess ubuntu slim

我目前正在TeamTreehouse.com上了解Slim框架,并遇到了一个我无法解决的问题。

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

  • DocumentRoot(/ home / daniel / src / public_html / treehouse / build_websites_php /)
    • 的index.php
    • 的.htaccess
    • composer.json
    • composer.lock
    • 供应商/
    • 模板/
      • 的index.html
      • contact.html

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

$app = new \Slim\Slim();

然后调用get()方法,然后调用render()以在url分别为localhost / treehouse / build_websites /和localhost / treehouse / build_websites / contact时呈现index.html和contact.html页面。 / p>

$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,只是服务器的默认值)。以下是我系统的一些规格:

  • Ubuntu 16.04.1 LTS
  • Apache 2.4.18
  • Slim 2.6.2
  • PHP 7.0.8

我的/ 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>

我尝试将AllowOverride All作为建议here添加到最后一个指令,然后我根本无法从服务器访问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

2 个答案:

答案 0 :(得分:0)

看起来您的Apache设置不允许.htaccess文件覆盖任何设置。将以下内容添加到apache2.conf

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

答案 1 :(得分:0)

好的哇,我终于明白了,感谢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指令之前就这样做了,所以我不确定它是否在解决问题方面起了作用,但我想我会为任何感兴趣的人记录它。

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