Check if a request belongs to dashboard section

Permalink
Hi,
What is the best way to check whether or not a request come from dashboard sections?
In 5.6, I used to write something like this:
$dashboardRequest = false;
$needdles = array('dashboard', 'tools');
foreach ($needdles as $needle) {
   if (stripos($_SERVER['REQUEST_URI'], $needle)) $dashboardRequest = true;   
}

This approach wasn't flawless even in 5.6, on account of packages or themes that might have contained an ajax request with 'tool' in their path. Or even pages that contain 'tool' in their slug. I wonder if anyone can suggest an ideal method to do this?

 
mnakalay replied on at Permalink Reply
mnakalay
This is not tested but it should work.

When you have a Request object you can use the getCurrentPage() function on it which will return a Page object. Then on that page object, you can use the function isAdminArea() which returns true if it's a dashboard page.

Hope it helps.
Herkool replied on at Permalink Reply
I want to use this in a package on_start method, so I suppose that 'getCurrentPage' method would not work at that stage.
mnakalay replied on at Permalink Reply
mnakalay
You are correct, it won't work. Actually at that stage, since you don't have a page object at all, trying to figure out if it's a dashboard page is not really relevant.

There is, however, a trick to make it work.

Inside the package's on_start function, use the on_start event and there you will have everything you need.
public function on_start()
{
    \Concrete\Core\Support\Facade\Events::addListener(
        'on_start', function ($event) {
            // do your thing
        }
    );
}