Troubleshooting routes

I am always having problems with writing the right routes. I am usually getting 404’s without understanding exactly why, and kind of finding the route via trial and error.

For example, considering I have <body data-homeurl="<?= $site->url() ?>"> see this simplified jquery ajax call:

$.ajax({
  type: "POST",
  url: $("body").data("homeurl") + "/pop-artwork",
  data: {
    id: id,
    all_ids: all_ids,
  },
  success: function (res) {
    res = JSON.parse(res);
    $("#modal-1 .modal-body").html(res);
  },
  error: function (jqXHR, exception) {
    console.log(jqXHR);
  },
});

In my localhost, this is pointing to:

http://localhost/~jaume/dev/nb/en/pop-artwork

And this is my attempt at a route to match that url

[
    'pattern' => '(:any)/pop-artwork',
    'action'  => function () {
        $id = get('id');
        $all_ids = get('all_ids');
        try {
            return json_encode(snippet('pop-artwork', array('id' => $id, 'all_ids' => $all_ids), true));
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
    }
],     

That results in a 404

I’ve also tried the following patterns, that as far as I understand, should work, but return 404 as well

'(en|es|ca)/pop-artwork'
'(:any)/(en|es|ca)/pop-artwork'
'(:all)pop-artwork'

So, is there any way to produce a list of the urls a route should match to via PHP?

And, in general what am I missing here?

Thank you

The routes are always based on the base site url, so in this case, should be just 'pattern' => 'pop-artwork', but since it's a multi-language site, set the language scope as well (in this case to*`):

Thank you,

This does not work either:

...
'pattern' => 'pop-artwork',
'language' => '*',
'action'  => function () {
...

Also, I assumed language was only needed if I wanted to render a page, while I am returning a snippet here.

Ok, in this case, as I was POSTing to the route, it seems I was missing the method in the route code, so:

        [
            'pattern' => '(:any)/pop-artwork',
            'method'  => 'POST',
            'action'  => function () {
                $id = get('id');
                $all_ids = get('all_ids');
                try {
                    return json_encode(snippet('pop-artwork', array('id' => $id, 'all_ids' => $all_ids), true));
                } catch (Exception $ex) {
                    return $ex->getMessage();
                }
            }
        ], 

It also works with the pattern (:all)/pop-artwork but not with pop-artwork solely.

Also, I found a way to troubleshoot the route is to change method to GET, then manually hitting the expected urls in the browser.

Cheers!