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

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

Open
Etheliene opened this issue Oct 20, 2021 · 2 comments
Open

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

Etheliene opened this issue Oct 20, 2021 · 2 comments

Comments

@Etheliene
Copy link

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
Copy link

mrfsrf commented Nov 20, 2021

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
Copy link

mrfsrf commented Nov 20, 2021

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)

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