I'm learning fatfree's route and found it behaves unexpected.
Here is my code in index.php:
$f3 = require_once(dirname(dirname(__FILE__)). '/lib/base.php');
$f3 = \Base::instance();
echo 'received uri: '.$_SERVER['REQUEST_URI'].'<br>';
$f3->route('GET /brew/@count',
function($f3,$params) {
echo $params['count'].' bottles of beer on the wall.';
}
);
$f3->run();
and here is the URL which I access: http://xx.xx.xx.xx:8090/brew/12
I get a 404 error:
received uri: /brew/12
Not Found
HTTP 404 (GET /12)
the strange thing is that the URI in F3 is now "/12" instead of "/brew/12" and I guess this is the issue.
When I check the base.php (3.6.5), $this->hive['BASE'] = "/brew" and $this->hive['PATH'] = "/12". But if F3 only uses $this->hive['PATH'] to match the predefined route, it won't be able to match them.
If I change the route to:
$f3->route('GET /brew',
and use the URL: http://xx.xx.xx.xx:8090/brew, then the route matches without issue. In this case, $this->hive['BASE'] = "" and $this->hive['PATH'] = "/brew". If F3 compares the $this->hive['PATH'] with predefined route, they match each other.
BTW, I'm using PHP's built-in web server and since $_SERVER['REQUEST_URI'] (which is used by base.php) returns the correct URI, I don't think there is anything wrong with the URL rewrite in my .htrouter.php.
Any idea? What did I miss here?
add the content of .htrouter.php here
<?php
#get the relative URL
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
#if request to a real file (such as a html, image, js, css) then leave it as it is
if ($uri !== '/' && file_exists(__DIR__ . $uri)) {
return false;
}
#if request virtual URL then pass it to the bootstrap file - index.php
$_GET['_url'] = $_SERVER['REQUEST_URI'];
require_once __DIR__ . './public/index.php';
Your issue is directly related to the way you're using the PHP built-in web server.
As stated in the PHP docs, here's how the server handles requests:
That means that, by default (without a router script), the web server is doing a pretty good job for routing unexisting URIs to your document root
index.phpfile.In other words, provided your file structure is like:
The following command is enough to start your server and dispatch the requests properly to the framework:
Or if you're running the command directly from the public/ folder:
Beware that the working directory of your application depends on the folder from which you call the command. In order to leverage this value, I strongly advise you to add
chdir(__DIR__);at the top of yourpublic/index.phpfile. This way, all subsequentrequirecalls will be relative to yourpublic/folder. For ex:$f3 = require('../lib/base.php');Routing file-style URIs
The built-in server, by default, won't pass unexisting file URIs to your
index.php, as stated in:So if you plan to define some routes with dots, such as:
Then it won't work because PHP won't pass the request to
index.php.In that case, you need to call a custom router, such as the
.htrouter.phpyou were trying to use. The only thing is that your.htrouter.phphas obviously been designed for a different framework (F3 doesn't care about$_GET['url']but cares about$_SERVER['SCRIPT_NAME'].Here's an exemple of
.htrouter.phpthat should work with F3:NB: the
$public_dirvariable should be set accordingly to the location of the.htrouter.phpfile.For example if you call:
it should be
$public_dir=__DIR__.'/public'.But if you call:
it should be
$public_dir=__DIR__.