simplePHPRouter
simplePHPRouter copied to clipboard
Example for named function with parameters
Looked at the examples, and reading the php docs about call_user_func_array i was missing the example, how it can be used with a pre-defined function, including parameters.
I would suggest a rather extensive example:
// Define function that handling routes, and has a default value for the variable
function my_func($my_var = 'default value') {
return 'This is a working pre-defined function. Parameter: '.$my_var;
}
// Add your index route
Route::add('/', function() {
return my_func();
});
// Add your first route
Route::add('/browse/(.*)', function($var) {
return my_func($var);
});
Sorry for this late answer. Holidays.
Try to use func_get_args() inside your predefined function. So you can have a variable number of arguments: https://www.php.net/manual/en/function.func-get-args.php
Not tested code:
function my_func() {
// Get all the function args without defining them inside the function head
$arg_list = func_get_args();
// Print all function args
print_r($arg_list);
// Return your content
if(count($arg_list)){
return 'This is a working pre-defined function. Parameter: '.$arg_list[0];
} else {
return 'This is a working pre-defined function. Parameter: Nor args given';
}
}
Route::add('/', 'my_func');
Route::add('/browse/(.*)', 'my_func');