Replacing the Dashboard page

A few years ago John Flatness wrote in this post “a plugin would be able to replace the dashboard page with a redirect to another page.”

Can you explain what I should add to my plugin to do that?

I don’t know if this is the right or best way, but here’s how I handled it. The goal was for users who login in with a researcher account to see a friendly welcome page rather than the admin dashboard.

    public function filterAdminDashboardStats($stats)
    {
        if ($this->userIsResearcher())
            $this->getRedirect()->gotoUrl(WEB_ROOT . '/welcome');

        return $stats;
    }

    protected function getRedirect()
    {
        return Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
    }

    protected function userIsResearcher()
    {
        $user = current_user();
        if (!$user)
            return false;

        return $user->role == 'researcher';
    }

This is how it works. The ‘admin_dashboard_stats’ filter is called early-on during processing of the dashboard page (admin/themes/default/index.php). I put the filterAdminDashboardStats function in a plugin. When called, the function aborts execution of the dashboard page by redirecting to a welcome page.

While effective, the solution does not seem very elegant. I’m wondering if there is a better approach.

That works.

The “more elegant” option would probably be to not redirect but instead add a custom route to the router that routes from the root URL / to your plugin’s page.

The redirect can also be done in a slightly more intended fashion by using a Zend controller plugin, which lets you run code that will happen before the dispatch occurs. (For example, the core itself uses a controller plugin to redirect users to the login page when they try to access the admin side).

Thanks for those suggestions. I looked at the core controller plugin, and also how the GuestUser plugin uses it. Is the idea to intercept the call to the dashboard and redirect to my own page from the controller plugin? That does seem like an improvement since the dashboard would never get invoked.

I have not yet worked with routes. Is the suggestion to define a custom route that somehow overrides the default route to the dashboard and instead loads a page in my plugin? I’ll need to look for an example of how that’s done.