How to set custom access for a custom page? For example, there is a page on the site, created before.

I need to set custom permission for it. 

Par example, if a user doesn't have a photo in his profile, he shouldn't have access to page "/forum".

On Drupal 7 it was possible via hook_menu_alter and set access control there.

Could you clarify me, how can I it on Drupal 8 ?
Here's an example which prevents anonymous users from resetting their password.

services.yml:

services:
  mymodule.route_subscriber:
    class: Drupal\mymodule\Routing\MymoduleRouteSubscriber
    tags: 
      - { name: event_subscriber }


src/Routing/MymoduleRouteSubscriber.php:

&lt;?php

namespace Drupal\mymodule\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class MymoduleRouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {

    if ($route = $collection-&gt;get('user.pass')) {
      $route-&gt;setRequirement('_custom_access', '\Drupal\mymodule\Access\ResetPasswordAccessCheck::access');      
    }
  }

}


src/Access/ResetPasswordAccessCheck.php:

&lt;?php
namespace Drupal\mymodule\Access;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;

class ResetPasswordAccessCheck {

  /**
   * A custom access check.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   */
  public function access(AccountInterface $account) {

    return $account-&gt;isAnonymous() ? AccessResult::forbidden() : AccessResult::allowed();

  }

}


I've updated the documentation to clarify what return value is expected: https://www.drupal.org/docs/8/api/routing-system/access-checking-on-routes

Forum access example

You only need the route subscriber in services.yml:

services:
  yrv_page_access.forum_route_subscriber:
    class: Drupal\yrv_page_access\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }


RouteSubscriber:

&lt;?php

namespace Drupal\yrv_page_access\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

class RouteSubscriber extends RouteSubscriberBase {

  protected function alterRoutes(RouteCollection $collection){
    if ($route = $collection-&gt;get('forum.index')){
      $route-&gt;setRequirement('_custom_access', '\Drupal\yrv_page_access\Access\ForumAccessCheck::access');
    }
  }
}


Access check:

&lt;?php

namespace Drupal\yrv_page_access\Access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;

/*
 * Custom access to the forum page
 */
class ForumAccessCheck {

  public function access(AccountInterface $account){
    return AccessResult::forbidden();
  }
}