I want to rebuild my objectbased PHP Scripts to an simple framework and have read some tutorials to learn from it. At the moment i stuck at the part how to pass the Params from url to function arguments (similar to here)
Passing the controller and method works fine but the first argument (id in my case) will not passed - on the correct index - with my code from urls like:
url: https: // Domain/controller/method/params
This is what i have to get the Informations from url:
public function __construct(){
//print_r($this->getUrl());
$url = $this->getUrl();
// Look in controllers for first value
if(file_exists('../app/controllers/' . ucwords($url[1]). '.php')){
// If exists, set as controller
$this->currentController = ucwords($url[1]);
// Unset 0 Index
unset($url[1]);
}
// Require the controller
require_once '../app/controllers/'. $this->currentController . '.php';
// Instantiate controller class
$this->currentController = new $this->currentController;
// Check for second part of url
if(isset($url[2])){
// Check to see if method exists in controller
if(method_exists($this->currentController, $url[2])){
$this->currentMethod = $url[2];
// Unset 2 index
unset($url[2]);
}
}
// Get params
$this->params = $url ? array_values($url) : [];
// Call a callback with array of params
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
As i understand all the magic happens here:
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
But if i try to echo the argument in an controller function, it is empty:
public function test($id){
echo $id;
}
What works is this, but the first argument from url here is passed as $id2:
public function test($id, $id2){
echo "First:";
echo $id; //empty
echo "Second:";
echo $id2; //(ID) 1
}
Url: https:// Domain/controller/test/1
So the thing i guess it is maybe related nginx rewrite rule and the result of get_url. Related the folder structur i tested some nginx rules to get the same result as with an htaccess apache version, but it is still different. So for passing the controller and methods i already had to set the array index +1 (see code above).
print_r($this->getUrl());
Result: Array ( [0] => [1] => controller [2] => test [3] => 1 )
Index [0] comes from the dir structur and catch the folder public. In the htaccess apache version this folder is not catched in the url array ... So is that maybe the cause for my issue?
Directory structur:
web (server doc root)
- public (public folder after using rewrites)
- app
The htaccess are the same as here. In addition to similar rules is the result on nginx everytime the same. This is my used rule at the moment:
location /{FOLDER} {
client_max_body_size 100M;
root {DOCROOT}/{FOLDER}public;
index index.php;
try_files $uri $uri/ /{FOLDER}index.php?url=$uri&$args;
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
{FASTCGIPASS}
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_param HTTP_AUTHORIZATION $http_authorization;
}
}
But maybe the issue is not releated to the nginx rules. I'm stuck here as I said already ...
Thanks!