Slim Framework routing doesn't work in my code - php

I'm new to this, I have a problem with following code:
<?php
require_once 'vendor/autoload.php';
require_once 'lib/controller/Root.php';
require_once 'lib/controller/General.php';
$app = new \Slim\Slim(array(
'debug' => true,
));
$root_controller = new Root();
$general_controller = new General();
$app->map('/', function() use ($app, $root_controller) {
$root_controller->index();
})->via('GET', 'POST');
$app->map('/vorstand', function() use ($app, $general_controller) {
$general_controller->vorstand();
})->via('GET', 'POST');
$app->map('/satzung', function() use ($app, $general_controller) {
$general_controller->satzung();
})->via('GET', 'POST');
$app->run();
?>
I'm using the slim framework and I can't call the routes localhost/vorstand or localhost/satzung everytime I get "Not found" And I have no explanation for that :/ only localhost which means the route "/" works.

For Apache server, two things need to be check.
1.The .htaccess file in the directory (same directory with your index.php) contains:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
2.You also need a directory directive to enable .htaccess files and allow the RewriteEngine directive to be used. This is sometimes done globally in the httpd.conf file, but its generally a good idea to limit the directive to just your virtual host by enclosing it in your VirtualHost configuration block. This is generally setup in your configuration in the form of:
<VirtualHost *:80>
ServerAdmin me#mysite.com
DocumentRoot "/path/www.mysite.com/public_html"
ServerName mysite.com
ServerAlias www.mysite.com
#ErrorLog "logs/mysite.com-error.log"
#CustomLog "logs/mysite.com-access.log" combined
<Directory "/path/www.mysite.com/public_html">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Add AllowOverride All to your DocumentRoot Directory.

Related

Symfony 5 : Error - No route found for "GET /<SiteConfigAcces>"

I developped a site with sympfony/phpp i want to deploy on a local server.
After the development on my own computer (where it works well), I put the project on a local server with apache, php 7.3.x, composer 2.0.0 under a debian 10 OS
All the transfer of the project from my computer to the server and the setting are well finished.
The project folder name is si2a. It is in the directory /var/www
I set the permissions access on this directory with ACL following a symfony doc guideline.
I set the site configuration file those the name is si2afama.conf. This last is in the directory /etc/apache2/sites-available. After setting, I activate the site, reload apache and clear the cache. Here is the si2afama.conf's contain :
<VirtualHost *:80>
ServerName si2afama
# ServerAlias www.si2afama.com
DocumentRoot /var/www/si2a/public
DirectoryIndex /index.php
<Directory /var/www/si2a/public>
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
<Directory /var/www/si2a/public/bundles>
FallbackResource disabled
</Directory>
ErrorLog /var/log/apache2/si2a_error.log
CustomLog /var/log/apache2/si2a_access.log combined
</VirtualHost>
When i try to access the site, i found this error :
No route found for "GET /si2afama"
Since, it is a Routing issus, here are some files's contain :
si2a/config/packages/routing.yaml
framework:
router:
utf8: true
si2a/config.routes.yaml
index:
path: /
controller: App\Controller\DefaultController::index
si2a/public/index.php.
<?php
use App\Kernel;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;
require dirname(__DIR__).'/config/bootstrap.php';
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
}
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts([$trustedHosts]);
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
I don't really know what to do in order to fix the issus.
May someone help me, please. Thank you.
Your vhost is related to your domain name and directory of source code,
if you want a route name like si2afama for your application, you have to add it to symfony routes.
# si2a/config.routes.yaml
index:
path: /
controller: App\Controller\DefaultController::index
si2afama:
path: /si2afama
controller: App\Controller\DefaultController::nameActionFromController
try to install this package composer require symfony/apache-pack it will create a .htaccess file that'll fix your problem

Internal Server Error Slim and .htaccess

First I need to say ya sorry for my bad english
I hope u can help me strangers.
I have created a domian in my linux Debian 8.
look it
<VirtualHost *:80>
ServerAdmin admin#admin.com
DocumentRoot "/var/www/saci"
ServerName saci.com
ServerAlias www.saci.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/saci">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I'm using slim and I've created a folder named 'app' on the folder 'saci' with a file start.php that got this:
<?php
use Slim\App;
session_cache_limiter(false);
session_start();
ini_set('display_errors', 'On');
define('INC_ROOT', dirname(__DIR__));
require INC_ROOT . '/vendor/autoload.php';
$app = new App();
$app->get('/test', function() {
echo "this is a test.";
});
?>
Now on my folder saci I've created a folder named Public with one index.php and one .htaccess.
so now look my roots
Var > www > saci > public
index.php <------------------index daaa
.htaccess <------------------.htaccess
var > www > saci > app
start.php <----------------start
so now my problem is here
I've got an index.php with this
<?php
require '../app/start.php';
$app->run();
and a .htaccess with this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
but when I go to my host www.saci.com I can see my index of/
with all my proyects folders But I cannot see the folder public, and if I go to www.saci.com/public
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#admin.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.10 (Debian) Server at www.saci.com Port 80
I rly DON'T HAVE IDEA of what is going wrong I need rly rly help
starting now
Thanks Steangers U rock never forget smile
Greetings from Venezuela

Raspberry PI - Slim API not working with Routes

I'm trying to built some Rest api on Raspberry PI model B with Slim Framework. The problem is that when i run the / route all work fine, when i try to run /test route i have a 404.
I think there are some problem with the .htaccess and rewrite rule, this is my structure of the api:
var/www/html/ is the root
--api
--libs
--Slim
--index.php
--.htaccess
and this is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [PT,L]
and this is my index.php slim app:
<?php
require '/var/www/html/api/libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/', function() use($app) {
echo "Root";
});
$app->get('/test', function() use($app) {
echo "test";
});
$app->run();
?>
Thanks in advance
Solution:
I've edited the file in etc/apache2/apache2.config changing these line
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None <----- this to All
Require all granted
</Directory>
then restarting apache2 with
sudo service apache2 restart
and i'll need also this line
sudo a2enmod rewrite
thanks #T0xicCode
Your apache configuration (check in /etc/apache2/) needs to have AllowOverride FileInfo or AllowOverride All for the directory where your files are (/var/www/html or /var/www/html/api). Otherwise apache will simply ignore the .htaccess and try to find a folder or a file named test. See the documentation.

Silex slugs cause 404 page not found

Following the documentation, Silex allows "slugs" to be passed in via the URL for use within your code.
The following example works:
$app = new Silex\Application();
$app->get('/', function () {
return 'HAI';
});
However, the following gives a 404 Not Found:
$app = new Silex\Application();
$app->get('/{slug}', function ($slug) {
return 'HAI' . $slug;
});
How can I fix this 404?
In case it's of any relevance, here's my Apache Vhost:
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/Silex/web"
DirectoryIndex index.php
<Directory "/var/www/Silex/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
...and my directory structure:
/src
|-- bootstrap.php
/tests
/vendor
/web
|-- index.php
It turns out that this was an Apache issue. It was assumed that you could either use a .htaccess file, or a vhost. You actually need to use both.
.htaccess:
FallbackResource /index.php
Note: You can only use FallbackResource if using Apache 2.2.16 or higher.
vhost
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/Silex/web"
DirectoryIndex index.php
<Directory "/var/www/Silex/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
An alternative is to place the contents of the .htaccess file (the FallbackResource directive) within the vhost itself, and get rid of the htaccess.
As soon as I added the .htaccess, the slug in my second example worked.

Blank pages using the SLIM PHP framework

I am currently developing a web application using the latest Slim version. Everything works perfectly on my professional computer (running on Ubuntu 12.04) but I would like to work on the project at home during my holidays. Thus, I have copied the project on my personal computer (running on mac os x 10.6.8). The problem is I always get blank pages. It does not look to be a rewrite problem since even with the ugly address, it does not work.
Please find below files and information that might be useful to fix the issue.
Folder structure (/Users/Yoann/Sites/DS)
-.htaccess
- index.php
- vendor/
- utils/
- services/
The utils and services directories contain some php classes and are not of interest to fix my issue.
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [QSA,L]
index.php (sample)
<?php
require_once 'vendor/autoload.php';
function autoload_class_multiple_directory($class_name)
{
# List all the class directories in the array.
$array_paths = array(
'utils/',
'services/'
);
foreach($array_paths as $path)
{
$file = sprintf('./%s%s.class.php', $path, strtolower($class_name));
if(is_file($file))
{
include_once $file;
}
}
}
spl_autoload_register('autoload_class_multiple_directory');
error_reporting(E_ALL);
ini_set('display_errors','On');
$app = new \Slim\Slim(array(
"MODE" => "debug"
));
/* **********************************
* ROUTE DEFINITIONS
* **********************************/
$arrDs = array("generator" => new Generator(),
"average" => new Average()
);
$app->get('/', function () use ($app,$arrDs){
echo "dans la fonction de base !!! <br/>";
foreach ($arrDs as $name => $obj){
echo $name ."<br/>";
}
});
$app->run();
?>
I am using a virtual host defined as
<VirtualHost *:80>
ServerAdmin [email blocked]
DocumentRoot "/Users/Yoann/Sites/DS"
ServerName ds.local
ErrorLog "/private/var/log/apache2/ds-error_log"
CustomLog "/private/var/log/apache2/ds-access_log" combined
<Directory "/Users/Yoann/Sites/DS">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
In development environment always set error_reporting to E_ALL and display_errors to true.
php.ini:
display_errors = On
error_reporting = E_ALL
You'll see all errors, warnings, etc ...

Categories