Get page of current panel route

#1

I’d like to use a route:before route to create a subpage, if it doesn’t exist already. Currently, I’m doing this to get the current page object:

return [
    'hooks' => [
        'route:before' => function ($route, $path, $method) {
            $uid = explode('/', $path);
            $uid = end($uid);
            $uid = str_replace('+', '/', $uid);

            try {
                $page = page($uid);
                // more code
            } catch (Exception $e) {}
        }
    ]
];

This feels very hacky and not very bulletproof. I tried page($path) but that doesn’t give me any result. Is there a core function to get the page object from the panel path?