Ich lerne derzeit über das schlanke Framework bei TeamTreehouse.com, und stieß auf ein Problem, das ich nicht lösen konnte.Slim 2.6.2 render() Funktioniert nur für Index.html
An dieser Stelle des Projekts haben wir Slim via Composer installiert und richten die Dateien .htaccess und index.php in unserem Dokumentenstamm ein (auf meinem Computer ist dies/home/daniel/src/public_html/troehouse/build_websites_php /) . In einem Vorlagenordner haben wir index.html und contact.html. Hier ist das Layout der Ordner.
- DocumentRoot (/ home/daniel/src/public_html/Treehouse/build_websites_php /)
- index.php
- .htaccess
- composer.json
- composer.lock
- Hersteller/
- Vorlagen/
- index.html
- contact.html
In index.php, instanziiert ich ein neues Slim-Objekt:
$app = new \Slim\Slim();
Und dann den get() -Methode aufrufe Rufen Sie anschließend render() auf, um die Seiten index.html und contact.html zu rendern, wenn die URL localhost/treehouse/build_websites/bzw. localhost/tree house/build_websites/contact lautet.
$app->get('/', function() use($app) {
$app->render('index.html');
});
$app->get('/contact', function() use($app) {
$app->render('contact.html');
});
Dann die App laufen:
$app->run();
Meine index.html Seite zeigt sich in Ordnung, aber ich bekomme einen Fehler 404 (nicht durch dünne, nur die Standard-Server), wenn ich versuche, und besuchen Sie die/Kontakt-URL Hier sind einige Spezifikationen von meinem System:
- Ubuntu 16.04.1 LTS
- Apache 2.4.18
- Schlanke 2.6.2
- PHP 7.0.8
Alles, was in meinem/Auf home/daniel/src/public_html/kann von Apache zugegriffen werden, da ich seit einem Jahr PHP-Skripte von dort verwende.
Ich habe die Vorschläge von here versucht (und den Server nach jedem Update auf conf.d oder anderen Dateien neu gestartet) und hatten kein Glück.
Jede Hilfe würde sehr geschätzt werden, ich habe nur PHP/Ubuntu/Apache für etwa ein Jahr verwendet, so vermisse ich wahrscheinlich etwas offensichtlich!
Hier ist der Index.php Datei:
<?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();
?>
Hier ist die .htacess Datei:
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]
Und hier sind verschiedene conf.d Dateien für Apache:
/etc/apache2/apache2.
# 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>
Ich habe versucht, AllowOverride All wie vorgeschlagen here zu der letzten Direktive hinzuzufügen und dann konnte ich überhaupt nicht auf PHP-Dateien vom Server zugreifen und bekam einen 500-Fehler statt eines 404-Fehlers.
/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 [email protected]
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 [email protected]
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
Als ich die * AllowOverride All hinzufügen * Richtlinie apache2.conf Der Server gibt einen Fehler von 500 zurück und ich kann überhaupt keine PHP-Skripte ausführen. –
Überprüfen Sie Ihre Fehlerprotokolle. –
Ich habe und fand den folgenden Fehler '[So Nov 13 10:37:51.054347 2016] [core: alert] [pid 10979] [client :: 1: 51900]/home/daniel/src/public_html /treehouse/build_websites_php/.htaccess: Ungültiger Befehl 'RewriteEngine', vielleicht falsch geschrieben oder von einem Modul nicht definiert in der Serverkonfiguration enthalten ' Ich versuchte diese Lösung' sudo a2enmod Rewrite & Sudo Service apache2 Neustart von [hier] (http://stackoverflow.com/questions/10144634/htaccess-invalid-command-rewriteengine-perhaps-misspelled -oder-definiert-by-am) aber keine Würfel, ich bekomme immer noch den 404 Fehler. –