Problem : include route from other file

Hi, i am new to slim framework 3.
just follow the instruction “hello world” … i try to write my code.
i put index.php, routes.php and .htaccess … in one folder

the problem is :
if i write my route in index.php … everything is running well.
but if i put my route in file “routes.php” … my browser just blank.

any suggestion ?

======
index.php

<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App();

require 'routes.php';

$app->run();

?>

=========
and route.php

<?php

$app->get('/', function (Request $request, Response $response)
{
$response->getBody()->write("Hello, friend");
return $response;
});

?>

========
.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Not sure if this is a typo here or a code error, but you have included routes.php while the file you listed is named route.php (without the s).

sorry … my bad.
file name is “routes.php”

Have you checked your server logs for an error? Since you are type hinting the Request and Response, have you included them use \Psr\Http\Message\ServerRequestInterface as Request; etc, in your routes.php file?

1 Like

Tim, you are good snipper :slight_smile:

yes … my fault is not include these lines in file “routes.php” :
After moved them from index.php to routes.php, everything is running well.

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

Thank you Tim.

1 Like