Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
[PHP]Check all POST's or GET's.[PHP]
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Heaplink" data-source="post: 226892" data-attributes="member: 8966"><p>You don't need to use <span style="font-size: 13px"><span style="font-family: 'monospace'"><span style="color: #0000bb">array_key_exists </span></span></span>It will exist, if it in there, but check if it is a callable (function). This way you can have anonymous functions in your array.</p><p> </p><p>Note: This is not the proper way of making a router, also consider using $_REQUEST instead.</p><p>[PHP]$requests = array(</p><p> 'post.login' => function(e) {</p><p> // Do some login here, probably have e return POST fields</p><p> },</p><p> </p><p> 'post.upload' => function(e) {</p><p> // Upload something, e could return data</p><p> },</p><p> </p><p> 'get.register' => register()</p><p> </p><p>);</p><p> </p><p>foreach($requests as $request => $callback) {</p><p> $data = null;</p><p> list(strtolower($type), $req) = explode($request, '.');</p><p> </p><p> if($type === 'get') {</p><p> $data = $_GET;</p><p> } else if ($type === 'post') {</p><p> $data = $_POST;</p><p> } else {</p><p> $data = null; // Not a type</p><p> }</p><p> // Here you could also check what kind of request, if it is a GET, POST or other</p><p> if(is_callable($callback)) {</p><p> $callback($data); // You can pass as many arguments you want.</p><p> }</p><p>}[/PHP]</p><p> </p><p><strong><span style="color: #ff0000">!!!</span> However I encourage you (and everyone) to use a real router like <a href="https://github.com/chriso/klein.php" target="_blank">Klein</a> or similar.</strong></p></blockquote><p></p>
[QUOTE="Heaplink, post: 226892, member: 8966"] You don't need to use [SIZE=13px][FONT=monospace][COLOR=#0000bb]array_key_exists [/COLOR][/FONT][/SIZE]It will exist, if it in there, but check if it is a callable (function). This way you can have anonymous functions in your array. Note: This is not the proper way of making a router, also consider using $_REQUEST instead. [PHP]$requests = array( 'post.login' => function(e) { // Do some login here, probably have e return POST fields }, 'post.upload' => function(e) { // Upload something, e could return data }, 'get.register' => register() ); foreach($requests as $request => $callback) { $data = null; list(strtolower($type), $req) = explode($request, '.'); if($type === 'get') { $data = $_GET; } else if ($type === 'post') { $data = $_POST; } else { $data = null; // Not a type } // Here you could also check what kind of request, if it is a GET, POST or other if(is_callable($callback)) { $callback($data); // You can pass as many arguments you want. } }[/PHP] [B][COLOR=#ff0000]!!![/COLOR] However I encourage you (and everyone) to use a real router like [URL='https://github.com/chriso/klein.php']Klein[/URL] or similar.[/B] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[PHP]Check all POST's or GET's.[PHP]
Top