Q:

Yii2: Setting layout for a specific controller

I want to change layout for all views inside my controller. So I want to set like this: 
class SiteController extends Controller {

public function __construct(){
   $this->layout = 'admin';
}
.....

But I am getting the following error:

Call to a member function getUniqueId() on null

A:

By default yii2 uses the main layout as the layout for project and it's controllers, but if you want to use another layout for a controller or change the layout name and use that layout for a controller to have to define a layout property in your controller class and give the name of your layout as the string value of that property.
This will change the layout of that controller to the demanded layout with your chosen name.
Here is the code in your case:
class SiteController extends Controller 
{
   public $layout = '[Your Layout Name]';

   .
   .
   .
}

P.S: Constructor is the method which runs whenever you create an instance of your class and using it in this case is not logical.

