steampixel / simplePHPRouter

This is a simple and small single class PHP router that can handel the whole url routing for your project.
MIT License
406 stars 117 forks source link

How to implement GET URL Query and 404 error page. #46

Closed shaziaijaz closed 3 years ago

shaziaijaz commented 3 years ago

hi, i like this code. i want help my question is How to implement GET URL Query and 404 error page.

shaziaijaz commented 3 years ago

/search?q=query

steampixel commented 3 years ago

Hi @shaziaijaz , please take a look at the index.php file. There you will find a 404-demo:

// Add a 404 not found route
Route::pathNotFound(function($path) {
  // Do not forget to send a status header back to the client
  // The router will not send any headers by default
  // So you will have the full flexibility to handle this case
  header('HTTP/1.0 404 Not Found');
  navi();
  echo 'Error 404 :-(<br>';
  echo 'The requested path "'.$path.'" was not found!';
});

And this could be your search boilerplate:

// Search example
Route::add('/search',function() {
  // Use the global $_GET PHP Array to access your q value.
  // Query the database
  // List the results
  // Do whatever you want
  echo 'You searched for '.$_GET['q'];
}, 'get');
shaziaijaz commented 3 years ago

thanks you