1

I try to run my Slim framework project on XAMPP and I use Apache configuration from Slim framework website.

When I open this URL http://localhost:8081/SlimAPIProject/public/hello/ayad I get this error:

Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:93 Stack trace:
#0 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\Routing\RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Http\ServerRequest))
#1 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle(Object(Slim\Http\ServerRequest))
#2 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\App.php(211): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#3 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\App.php(195): Slim\App->handle(Object(Slim\Http\ServerRequest))
#4 C:\xampp\htdocs\SlimAPIProject\public\index.php(16): Slim\App->run()
#5 {main} thrown in C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 93

This is my index.php

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->run();
Nima
  • 3,309
  • 6
  • 27
  • 44
  • I think you've skipped the part where you point Apache to the `public` folder. Configuring Slim with private folders in the document tree is really hard as it's not the way it's been designed to run. – Álvaro González Apr 27 '20 at 13:23
  • As ÁlvaroGonzález said, you should set `DocumentRoot` to point to `C:\xampp\htdocs\SlimAPIProject\public` and then try opening `http://localhost:8081/hello/ayad` (without /SlimAPIProject/public part). You also need to make sure you put .htaccess file in your document root (public directory) – Nima Apr 27 '20 at 14:19
  • how i can set DocumentRoot as what you say – Ayad Muayad Apr 27 '20 at 15:01
  • this is my .htaccess code RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L] – Ayad Muayad Apr 27 '20 at 15:02
  • Have a look at this question https://stackoverflow.com/questions/10157333/xampp-change-document-root – Nima Apr 27 '20 at 15:37
  • after i try many solution and set DocumentRoot . i am sure now that i have no problem with xampp or the virtual host . but the error is still shown and i do not understand what is this error means . – Ayad Muayad Apr 27 '20 at 18:42
  • You register `'/hello/{name}'` but try to load `/SlimAPIProject/public/hello/ayad` and error message shows suspicious paths like `C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php` (`htdocs` is the public root in Apache but `vendor` is a private directory). Which of the two set-ups are you trying to accomplish? – Álvaro González Apr 28 '20 at 07:41
  • i don't know what you mean . but the link is open when i use php builtin server and not open with xampp . i don't know if i have to set more setting in xampp or .htaccess to let it open with xampp . – Ayad Muayad Apr 28 '20 at 19:04
  • So far it's still unclear whether you're trying to load the site through `http://localhost:8081/hello/ayad` (as Slim recommends) or through `http://localhost:8081/SlimAPIProject/public/hello/ayad`. – Álvaro González Apr 29 '20 at 09:08
  • on the php builtin server i load the site through http://localhost:8081/hello/ayad and it is work fine . but on the xampp i load the site through http://localhost:8081/SlimAPIProject/public/hello/ayad and it is not work with the error in my main equation and also http://localhost:8081/hello/ayad is not work with error not found page . – Ayad Muayad Apr 29 '20 at 12:51
  • There're two alternatives to choose from, you try both, of course one is not going to work! You don't have any reason to even try `http://localhost:8081/SlimAPIProject/public/hello/ayad`; stop doing it. Stick to the Slim way. – Álvaro González Apr 30 '20 at 13:01
  • sir the two way is slim way and in the tutorial on the web the two way is working and i want to know why the second way is not working with me that is all . thanks for helping – Ayad Muayad Apr 30 '20 at 18:20
  • `http://localhost:8081/hello/ayad/` points to `C:\xampp\htdocs\SlimAPIProject\public\index.php`. `http://localhost:8081/SlimAPIProject/public/hello/ayad/` points to `C:\xampp\htdocs\index.php`. If you want to access the same resources in either paths you need to figure out a really complex set up (symbolic links, URL rewriting or keeping two copies of every file in your codebase). It won't happen by default for the same reason that `http://localhost:8081/foo` will not equal `http://localhost:8081/bar`. – Álvaro González May 01 '20 at 09:01
  • thanks sir for trying to help . – Ayad Muayad May 01 '20 at 15:26

1 Answers1

4

This could happen if you are running Slim in a subdirectory of your web root. Slim needs to know which part of the URL should be considered as the base URL of your App so that everything coming after that will be treated as a route.

I have seen many solutions out there and none of them worked for me.

Also, you want your app to be portable so that when you deploy it, you won't need to fiddle with some settings that are different across servers. So I came up with the following solution that doesn't require any change outside of the main Slim application's entrypoint.

<?php

...

// Instantiate App
$app = AppFactory::create();

// Set the base path of the Slim App
$basePath = str_replace('/' . basename(__FILE__), '', $_SERVER['SCRIPT_NAME']);
$app = $app->setBasePath($basePath);


This will dynamically calculate the value that should be used as base bath and will work even if you move the script to another location or if you rename it.

asiby
  • 3,229
  • 29
  • 32