PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 18, 2022

[FIXED] Yii2: Setting layout for a specific controller

 February 18, 2022     php, yii, yii2, yii2-advanced-app     No comments   

Issue

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


Solution

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.



Answered By - TheDevWay
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing