Upstatement / routes

Simple routing for WordPress
188 stars 38 forks source link

Routes::load() loads index.php file instead of defined file #34

Open Etheliene opened 2 years ago

Etheliene commented 2 years ago

I have the following routes in my functions.php:


// Create initial worldmap
Routes::map('verzeichnis', function() {
    Routes::load(get_stylesheet_directory() . '/directory/directory.php', null, null, 200);
});
// Create fixated slugstructure
Routes::map('verzeichnis/:country', function($params) {
    $query = 'posts_per_page=30&country='.$params['country'];
    Routes::load(get_stylesheet_directory() . '/directory/directory-country.php', $params, $query, 200);
});

Routes::map('verzeichnis/:country/:city', function($params) {
    $query = 'posts_per_page=30&country='.$params['country'].'&city='.$params['city'];
    Routes::load('directory/directory-city.php', $params, $query, 200);
});

Routes::map('verzeichnis/:country/:city/:rechtsgebiet', function($params) {
    $query = 'posts_per_page=30&country='.$params['country'].'&city='.$params['city'].'&rechtsgebiet='.$params['rechtsgebiet'];
    Routes::load('directory/directory-schwerpunkt.php', null, $query, 200);
});

According to the documentation everything should work fine and I can load the initial Route 'verzeichnis' - but as soon as I try to load one of the sub-routes I get the default behaviour of the index.php which shows my posts.

What I am wondering is if it would be possible to define a single dynamic route eg.

Routes::map('verzeichnis/:country/:city/:rechtsgebiet', fn()); 

where the parameters that are here dynamic are optional but not necessary.

Please update the documentation and or let me know where I could find some sort of support if this is not the right place. Thanks in advance!

mrfsrf commented 2 years ago

Regarding optional route patterns, try /:one?/:two?/:three? Haven't tried it but I think it should work.

Are you sure your WP_Query is ok?

Update:

~~Looks like I was wrong, there is no logic for checking for optional patterns. You can Extend Routes class and add that logic.~~ Optional route patterns work (Didn't look in AltoRouter.php)

mrfsrf commented 2 years ago

you can write it like this:

// Routes::map('verzeichnis', 'my_cb');
// Routes::map('verzeichnis/:country', 'my_cb');
// Routes::map('verzeichnis/:country/:city', 'my_cb');
// Routes::map('verzeichnis/:country/:city/:rechtsgebiet', 'my_cb');
Routes::map('verzeichnis/:country?/:city?/:rechtsgebiet?', 'my_cb');

function my_cb($params) {

  foreach($params as $key => $value){
    switch($key){
      case 'rechtsgebiet':
        $filename = '-schwerpunk';
        break;
      case 'city':
        $filename = '-city';
        break;
      case 'country':
        $filename = '-country';
        break;
      default:
        $filename = '';
        break;
    }
  }

  $query = array(
    'posts_per_page'  => 30,
    'country'         => $params['country'], // is this correct WP_Query?
    'city'            => $params['city'],
    'rechtsgebiet'    => $params['rechtsgebiet'],
    // Add this if you want to optimize query
    'no_found_rows'   => true,
    'ignore_sticky_posts' => true,
  );

  Routes::load(get_stylesheet_directory() . '/directory/directory' .$filename. '.php', $params, $query);
}

From the Documentation

$query The query you want to use, it can accept a string or array just like Timber::get_posts -- use the standard WP_Query syntax (or a WP_Query object too)