dprc88435 2016-11-13 14:43
浏览 63
已采纳

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

I'm currently learning about the Slim framework over at TeamTreehouse.com, and ran into an issue that I haven't been able to resolve.

At this point in the project, we have installed Slim via Composer and setup .htaccess and index.php files in our document root (which on my computer is /home/daniel/src/public_html/treehouse/build_websites_php/). In a templates folder we have index.html and contact.html. Here is the layout of the folders.

  • DocumentRoot (/home/daniel/src/public_html/treehouse/build_websites_php/)
    • index.php
    • .htaccess
    • composer.json
    • composer.lock
    • vendor/
    • templates/
      • index.html
      • contact.html

In index.php, I instantiate a new Slim object:

$app = new \Slim\Slim();

And then call the get() method and then call render() to render the index.html and contact.html pages when the url is localhost/treehouse/build_websites/ and localhost/treehouse/build_websites/contact, respectively.

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

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

Then run the app:

$app->run();

My index.html page shows up fine, but I get a 404 error (not through Slim, just the server's default) when I try and visit the /contact url. Here are some specs from my system:

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

Anything in my /home/daniel/src/public_html/ directory can be accessed by Apache, as I've run PHP scripts from in there for the past year.

I've tried the suggestions from here (and restarted the server after each update to conf.d or other files) and have had no luck.

Any help would be greatly appreciated, I've only been using PHP/Ubuntu/Apache for about a year, so I'm probably missing something obvious!

Here is the index.php file:

<?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();

?>

Here is the .htacess file:

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]

And here are various conf.d files for Apache:

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

I tried adding AllowOverride All as suggested here to the last directive and then I wasn't able to access PHP files from the server at all and got a 500 error instead of a 404 error.

/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条回答 默认 最新

  • dongshuang0011 2016-11-14 04:20
    关注

    OK wow I finally figured it out thanks to Mika pointing me in the right direction. It turns out, my /etc/apache2/sites-available/mysite.conf file needed the following directive:

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

    I had tried adding that directive to /etc/apache2/apache2.conf in addition to the other directives like so:

    <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>
    

    But the AllowOverride All from above through a 500 error from the server. Apparently, it had to be by itself in the /etc/apache2/sites-available/mysite.conf file, who knew!

    I also ran sudo a2enmod rewrite && sudo /etc/init.d/apache2 restart to make sure the mod_rewrite was loaded after discovering this error message (thanks to Mika for pointing out to check the log files!):

    [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
    

    I did that before adding the AllowOverride All directive, so I'm not sure if it played a part in solving the problem, but I figured I would document it for any interested.

    These sites had valuable information on how to finally solve the issue:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!