How to set dynamic criteria for endpoint with the Element API plugin? How can I set the criteria for an endpoint based on the current category view?

My elementapi.php looks like this where I can pass the slug of a category url to force a return of entries from the slug name.

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;slug = "albania";
$category = $criteria-&gt;first();

return [
                'endpoints' =&gt; [
                    'directory.json' =&gt; [
                        'elementType' =&gt; 'Entry',
                        'criteria' =&gt; [
                                'section' =&gt; 'directory',
                                'limit' =&gt; null,
                            ],
                        'paginate' =&gt; false,
                        'transformer' =&gt; function(EntryModel $entry) {
                            return [
                                'title' =&gt; $entry-&gt;title,
                                'mapinfo' =&gt; $entry-&gt;mapaddress,
                                'url' =&gt; $entry-&gt;slug
                            ];
                        },
                    ],
                    'directory-category.json' =&gt; [
                        'elementType' =&gt; 'Entry',
                        'criteria' =&gt; [
                            'section' =&gt; 'directory',
                            'relatedTo' =&gt; ['targetElement' =&gt; $category],
                            'limit' =&gt; null,
                        ],
                        'paginate' =&gt; false,
                        'transformer' =&gt; function(EntryModel $entry) {
                            return [
                                'title' =&gt; $entry-&gt;title,
                                'mapinfo' =&gt; $entry-&gt;mapaddress,
                                'url' =&gt; $entry-&gt;slug
                            ];
                        },
                    ],
                    'seafarers-directory/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
                        return [
                            'elementType' =&gt; 'Entry',
                            'criteria' =&gt; ['id' =&gt; $entryId],
                            'first' =&gt; true,
                            'transformer' =&gt; function(EntryModel $entry) {
                                return [
                                    'title' =&gt; $entry-&gt;title,
                                    'url' =&gt; $entry-&gt;url,
                                    'body' =&gt; $entry-&gt;body,
                                    'mapinfo' =&gt; $entry-&gt;mapaddress
                                ];
                            },
                        ];
                    },
                ]
            ];


I tried setting the criteria category slug name with

craft()-&gt;urlManager-&gt;getMatchedElement(); 


but it always returned nothing.

Can a dynamic category be set for the criteria from the existing url like the dynamic entry is?
Finally figured this out.
By using the variable slug in the endpoint URL I was able to listen for the category in the currently viewed category slug and pull the data i needed for my google map. The trick is to typomake the end point a function - i was trying to set the criteria outside of the endpoint which is, in retrospect, silly.

'locations/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {

                        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
                        $criteria-&gt;slug = $slug;
                        $category = $criteria-&gt;first();
                        return [
                            'elementType' =&gt; 'Entry',
                            'criteria' =&gt; [
                                'section' =&gt; 'directory',
                                'relatedTo' =&gt; ['targetElement' =&gt; $category],
                                'limit' =&gt; null,
                            ],
                            'transformer' =&gt; function(EntryModel $entry) {
                                return [
                                    'title' =&gt; $entry-&gt;title,
                                    'mapinfo' =&gt; $entry-&gt;mapaddress,
                                    'url' =&gt; $entry-&gt;slug
                                ];
                            },
                        ];
                    },