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


#1

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 = []) {
        parent::__construct();

        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
        $this->addAuth();


        //set date format etc
        $this->setPersistenceFormat();

        //if user is logged in correctly
        if(!$this->auth->user->loaded()) {
            $this->addLoginForm();
            exit;
        }
        //no calendar selected? show calendar selection page
        if(!$this->_selectCalendar()) {
            $this->addCalendarSelector();
            exit;
        }
        //user does not have neccessary rights for that page
        if(!in_array($this->auth->user->getRoleForCalendar(), $user_rights)) {
            $this->addInsufficentRightsPage();
            exit;
        }
        else {
            $this->initLayout(new \CC\View\CCAdminLayout());
            //add custom CSS and JS files
            $this->requireCSS('css/cc.css');
            $this->requireJS('js/cc.js');
            $this->addMenu();
        }
    }

#2

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.


#3

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.