Route restrict access for current user How do I restrict access to the route by current user in Drupal 8?

I want to create a new tab in user profile path: '/user/{user}/my-cool-tab', which should be accessible by current user only. For example:


path /user/5/my-cool-tab should be available only for UID=5
path /user/7/my-cool-tab should be available only for UID=7 
etc.


Which way is the simplest and correct for that?
You need an access check where you compare the passed user id with the current user, here's an example from simplenews:

public function checkAccess(UserInterface $user) {
  $account = $this-&gt;currentUser();

  return AccessResult::allowedIfHasPermission($account, 'administer simplenews subscriptions')
    -&gt;orIf(AccessResult::allowedIfHasPermission($account, 'subscribe to newsletters')
      -&gt;andIf(AccessResult::allowedIf($user-&gt;id() == $account-&gt;id())));
}


Note that this includes and admin permission check, for admins that can actually see the page for other users. 

If you do not need that, you could also just make your route /my-cool-page and use the current user for whatever you are doing. With D8's routing system, you can even make local task appear on the user page if you want to.