atk4\ui\App: Need db connection to determine which layout to use, but initLayout() has to be called prior to dbConnect()


Hi there,

after updating UI from 1.6.0 to 1.6.2 I have a nasty problem:
To init the correct layout, I need a db Connection first. But App->dbConnect() now throws an Exception (actually its add()) if no layout is set:

So if I set a layout prior to dbConnect(), it could happen that iโ€™d have to destroy $app->layout object again, which of course is not so nice. Prior to this update (I think its since 1.6.1) this didnt happen.

Hereโ€™s an excerpt from my custom app class. Any hints how to get round that problem?

 public function __construct(array $user_rights = []) {

        if(count($user_rights) === 0) {
            throw new \atk4\data\Exception('User rights array must always be passed to constructor of '.__CLASS__);
        $this->userRights = $user_rights;

        $this->template_dir = [FILE_BASE_PATH.'template/cc', FILE_BASE_PATH.'vendor/atk4/ui/template/semantic-ui'];

        //Database Connection
        $this->dbConnect($_ENV['DB_STRING'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);

        //Add auth class

        //set date format etc

        //if user is logged in correctly
        if(!$this->auth->user->loaded()) {
        //no calendar selected? show calendar selection page
        if(!$this->_selectCalendar()) {
        //user does not have neccessary rights for that page
        if(!in_array($this->auth->user->getRoleForCalendar(), $user_rights)) {
        else {
            $this->initLayout(new \CC\View\CCAdminLayout());
            //add custom CSS and JS files

Hello. I had a very similar problem. The way I fixed it is to set the most used layout before dbConnect and then if I need to change it. (i.e. user has no yet been logged in), I change it after the dbConnect.


Thats my current solution too. But of course it would be nicer if it worked without setting a layout prior to dbConnect(). Would save lines of code and also 1 object in case first layout is destroyed again to set another.


Hello there,

This issue makes the udemy class 18 fail when the user moves the dbConnect() statement from line 25 to line 9 (minutes 3:10 - 3:15) so it would be important to fix it in my opinion.

Edit: class link:


thanks for noticing this!

probably worth doing something about it :smiley: