Closed kingx619 closed 3 years ago
Hi! Thanks for this question.
The reason is quite simple but not obvious.
The call $router->fetchActions($mySite = new \app\core\MySite());
creates route index
and it will be called with method $router->callRoute('/');
But when you add route manually $router->addRoute( '/' , [ $mySite , 'someOtherPage' ] );
it creates route with name empty string
and that is why it is not called. You need to $router->addRoute( 'index' , [ $mySite , 'someOtherPage' ] );
That is how it works in the version of Mezon Router wich you are using now.
But I understand that this behavour is quite inconsistent and since v.1.2.13 the call $router->addRoute( '/' , [ $mySite , 'someOtherPage' ] );
will work fine too )
Thanks for your question. If you will face other problems please feel free to create new issues.
Can I close this one?
I am still unable to call this method public function someOtherPage() from class MySite. Can you please tell how to call this method using manual callback for url http://127.0.0.1:8080/abc
what I am doing is this require_once DIR.'/../vendor/autoload.php';
$router = new \Mezon\Router\Router(); $router->fetchActions($mySite = new \app\core\MySite());
$router->addRoute( '/abc' , [ $mySite , 'someOtherPage' ] ); // No output
How to fix this
First of all try to call it like this:
http://127.0.0.1:8080/?r=abc
Is it working?
I have verified. It is not working even after adding the query string like you mentioned
Do you call
$router->callRoute($_GET['r']);
After all routes were added?
I did not add that line after all routes were added. Now I tried that but I am getting an error like this
$router->addRoute( '/abc' , [ $mySite , 'someOtherPage' ] ); $router->callRoute($_GET['r']);
Warning: Undefined array key "r" in C:\mezon_router\public\index.php on line 11. I even replaced r with abc..:D but still there is no output
Fatal error: Uncaught Exception: The processor was not found for the route in GET : ; index, contacts, abc; , ; POST : ; index, contacts; , ; PUT : ; DELETE : ; OPTION : ; PATCH : in C:\mezon_router\vendor\mezon\router\Mezon\Router\Router.php:83 Stack trace: #0 [internal function]: Mezon\Router\Router->noProcessorFoundErrorHandler('') #1 C:\mezon_router\vendor\mezon\router\Mezon\Router\Router.php(126): call_user_func(Array, '') #2 C:\mezon_router\public\index.php(11): Mezon\Router\Router->callRoute('') #3 {main} thrown in C:\mezon_router\vendor\mezon\router\Mezon\Router\Router.php on line 83
Well as I see the route 'abc' was added for the GET method.
Insert please this code
var_dump($_GET);die(0);
before $router->callRoute($_GET['r']);
And try http://127.0.0.1:8080/?r=abc
What web server do you use?
I use PHP in built development webserver php -S localhost:8080 . I did as you said
$router->addRoute( '/abc/' , [ $mySite , 'someOtherPage' ] ); var_dump($_GET);die(0); $router->callRoute($_GET['r']);
and got array(0) { } after making the var_dump. I am using PHP 8.0. When I added the query string as you mentioned(http://127.0.0.1:8080/?r=abc) . I got this output on var_dump :::: array(1) { ["r"]=> string(3) "abc" }
Nice, remove the var_dump command and try to run http://127.0.0.1:8080/?r=abc
What is the output?
Here is the var dump
array:25 [▼ "DOCUMENT_ROOT" => "C:\mezon_router\public" "REMOTE_ADDR" => "127.0.0.1" "REMOTE_PORT" => "55745" "SERVER_SOFTWARE" => "PHP 8.0.0 Development Server" "SERVER_PROTOCOL" => "HTTP/1.1" "SERVER_NAME" => "127.0.0.1" "SERVER_PORT" => "8080" "REQUEST_URI" => "/" "REQUEST_METHOD" => "GET" "SCRIPT_NAME" => "/index.php" "SCRIPT_FILENAME" => "C:\mezon_router\public\index.php" "PHP_SELF" => "/index.php" "HTTP_HOST" => "127.0.0.1:8080" "HTTP_CONNECTION" => "keep-alive" "HTTP_UPGRADE_INSECURE_REQUESTS" => "1" "HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9" "HTTP_SEC_FETCH_SITE" => "none" "HTTP_SEC_FETCH_MODE" => "navigate" "HTTP_SEC_FETCH_USER" => "?1" "HTTP_SEC_FETCH_DEST" => "document" "HTTP_ACCEPT_ENCODING" => "gzip, deflate, br" "HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.9" "REQUEST_TIME_FLOAT" => 1608042575.2076 "REQUEST_TIME" => 1608042575 ]
This Time I tried this as you said $router->addRoute( '/abc' , [ $mySite , 'someOtherPage' ] ); $router->callRoute($_GET['r']);
and http://127.0.0.1:8080/?r=abc // Now working
And got the output.. but why it is not working on normal route and working only after adding the query string??
but why it is not working on normal route and working only after adding the query string??
Try this:
$router->callRoute($_SERVER['REQUEST_URI']);
Tried this $router->callRoute($_SERVER['REQUEST_URI']);
and got the output from actionIndex
"This is the main page of our simple sitex" .
I am sorry, with this http://127.0.0.1:8080/abc
Why? Whats wrong...Some problem with the PHP Development server??
I can figure out that that the addRoute function accepts first argument as Route('/abc') and second argument as callback. In my case callback is [mySite , 'someOtherPage'] . mySite is object and someOtherPage is the method and you are resolving it with call_user_func . So it should call method someOtherPage on class MySite and should try to echo the message inside the function someOtherPage..But thats not happening..I dont know why
Why? Whats wrong...Some problem with the PHP Development server??
Everithing is OK.
You can chose how to pass route.
You can pass it via 'r' parameter and you should use $router->callRoute($_GET['r']);
(for http://127.0.0.1:8080/?r=abc)
You can pass it via REQUEST_URI and in this case you should use $router->callRoute($_SERVER['REQUEST_URI']);
(for http://127.0.0.1:8080/abc)
Both cases are valid and depend on wich web server you use.
Okay thanks a lot !!! I got your point...:D
You can add this to the documentation its not mentioned anywhere :GET['r] and $router->callRoute($_SERVER['REQUEST_URI']);. One more thing I forgot to say.. Please remove the benchmarks of your router and make a separate repository that contains the router and benchmarks. This repository should only contain the code that can also be pushed on production server. Its my suggestion. The repository should only contain the necessary code and nothing else.. You can close this issue after reading it
@kingx619 Hi! Have moved documentation to separate branch - https://github.com/alexdodonov/mezon-router/tree/doc
Hope it will be more convenient for you.
Nice....This is the best move you have made...Thanks for that...Alex!!
)
this is How my index.php file looks like
require_once DIR.'/../vendor/autoload.php';
$router = new \Mezon\Router\Router(); $router->fetchActions($mySite = new \app\core\MySite()); //$router->callRoute('/'); //Working //$router->callRoute('/contacts/'); //Working
$router->addRoute( '/' , [ $mySite , 'someOtherPage' ] ); //Not Working
This method is not working and i am not getting any output on the screen. Can you tell me the issue with this approach why its not working
This is how my class File looks like : namespace app\core;
class MySite { /**
Main page. */ public function actionIndex() { echo 'This is the main page of our simple sitex'; }
/**
Contacts page. */ public function actionContacts() { echo 'This is the "Contacts" page'; }
/**
Some custom action handler. */ public function someOtherPage() { echo 'Some other page of our site'; }
public static function someStaticMethod() { echo 'Result of static method'; } }