Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send a GET request? #5

Closed
Jahak opened this issue Oct 19, 2014 · 5 comments
Closed

How to send a GET request? #5

Jahak opened this issue Oct 19, 2014 · 5 comments

Comments

@Jahak
Copy link

Jahak commented Oct 19, 2014

Eg

' ; }); µ::GET('/', function() { echo $_GET['username']; }); ?>

It is impossible to get a username.

What's wrong? Help me please.

@lastguest
Copy link
Owner

$_GET is used to retrieve parameters passed by the URL query string.
http://php.net/manual/it/reserved.variables.get.php

For instance, if you call your script with this request:

http://example.com/foo?username=bar

the $_GET['username'] variable is assigned to bar.

By the way, in your script you are redeclaring the GET / route 2 times, and you are not invoking the route dispatcher µ::_(); after the route definitions.

@Jahak
Copy link
Author

Jahak commented Oct 19, 2014

@lastguest , added µ::_();
Please tell me how to get the user name using $ _GET using your PHP micro-router?

@Jahak
Copy link
Author

Jahak commented Oct 19, 2014

write a simple example of the method $_GET

@lastguest
Copy link
Owner

Just try this :

µ::GET('/', function(){
    var_dump( $_GET );
});
µ::_();

Now you can call it in your browser like this :

http://localhost:8080/?name=hello+world&number=123

array(2) { ["name"]=> string(11) "hello world" ["number"]=> string(3) "123" }

Important:

• You must setup a front-controller pattern for redirecting all requests to the index.php file.
If you are using Apache you must define an .htaccess like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php

• Some web server configurations appends the query string part of the request URL to the $_SERVER['REQUEST_URI'] global, therefore breaking routing on parameters passing.
In these cases you must replace the REQUEST_URI text in µ class definition with REDIRECT_URL.

A more stable approach is replacing the getenv(REQUEST_URI) with strtok(getenv(REQUEST_URI),'?') in µ class definition.

Please, remember that this is an experiment just for fun. Do not use it in production!

@Jahak
Copy link
Author

Jahak commented Oct 19, 2014

@lastguest , well, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants