Welcome Guest, Not a member yet? Register   Sign In
Problem in accessing query in construct
#1

Hi,

I'm pretty new using CI, and I'm developing a system that should be able to enable/disable access to pages through AdminCP, but I'm having some problems when trying to implement it.
The problem is that when I 

First here's my BaseController
PHP Code:
<?php

namespace App\Controllers;

use 
App\Libraries\Twig;
use 
App\Models\SettingsModel;
use 
App\Models\UsersModel;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *    class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */

class BaseController extends Controller {
 
/**
 * Instance of the main Request object.
 *
 * @var IncomingRequest|CLIRequest
 */
 
protected $request;

 
/**
 * An array of helpers to be loaded automatically upon
 * class instantiation. These helpers will be available
 * to all other controllers that extend BaseController.
 *
 * @var array
 */
 
protected $helpers = [];

 
/**
 * Instance of session service
 */
 
protected $session;

 
/**
 * Instace of site settings
 */
 
protected $settings;

 
/**
 * Instance of user data
 */
 
protected $user;

 
/**
 * Constructor.
 *
 * @param RequestInterface  $request
 * @param ResponseInterface $response
 * @param LoggerInterface  $logger
 */
 
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger) {
 
// Do Not Edit This Line
 
parent::initController($request$response$logger);

 
//--------------------------------------------------------------------
 // Preload any models, libraries, etc, here.
 //--------------------------------------------------------------------
 // E.g.: $this->session = \Config\Services::session();

 
$this->session = \Config\Services::session();

 
$settingsModel = new SettingsModel;

 
$this->settings $settingsModel->getSettings();

 
$this->twig = new Twig;

 
$router service('router');

 
helper('text');

 
$this->twig->addGlobal('controller'$router->methodName());
 
$this->twig->addGlobal('messages'$this->session->get('messages'));
 
$this->twig->addGlobal('logged_in'$this->session->get('logged_in'));

 if (
$this->session->get('logged_in')) {
 
$usersModel = new UsersModel;

 
$this->user $usersModel->where('id'$this->session->user_id)->first();

 if (
$this->user->last_ip !== $this->request->getIPAddress()) {
 return 
redirect()->to('/auth/logout');
 }
 }
 }


My problem is that in my page Controller I can't access the $settings var in __construct, it always returns null.

And here's the page controller
PHP Code:
<?php

namespace App\Controllers\Users;

use 
App\Controllers\BaseController;

class 
Forum extends BaseController {

    public function __construct() {
        if (!$this->settings['forum']) {
            return redirect()->to('/user/dashboard');
        }
    }

    public function index() {
        return $this->twig->render('forum/index');
    }


Same happens when I try to check if user last_ip is equal to request IP address to logout if it doesn't match. It's maybe a problem in my code (most probably), but even reading the docs I wasn't able to figure out how to handle this.

Thanks in advance.
Reply
#2

Add a method in your BaseController to get the settings.
Example:
PHP Code:
protected function getSettings()
{
    return $this->settings;

Try that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(08-08-2021, 01:18 AM)InsiteFX Wrote: Add a method in your BaseController to get the settings.
Example:
PHP Code:
protected function getSettings()
{
    return $this->settings;

Try that.

I tried to add this method in my BaseController, then did a var_dump in the another controller construct function but it returned null anyway.

I think the best workaround to this problem is using the CI4 Filters, but as I fetch all settings from database on BaseController, is there any way to use the data from this fetch instead of fetching it again on Filters?

The reason why I wanted to filter it on construct function is because I'll need to add this kind of filter to many pages individually, so creating Filters I'll need to create too many filters to verify if each page is enabled, is there any way to do this in a more "automatic"?
Reply
#4

(08-08-2021, 03:18 PM)neto737 Wrote:
(08-08-2021, 01:18 AM)InsiteFX Wrote: Add a method in your BaseController to get the settings.
Example:
PHP Code:
protected function getSettings()
{
    return $this->settings;

Try that.

I tried to add this method in my BaseController, then did a var_dump in the another controller construct function but it returned null anyway.

I think the best workaround to this problem is using the CI4 Filters, but as I fetch all settings from database on BaseController, is there any way to use the data from this fetch instead of fetching it again on Filters?

The reason why I wanted to filter it on construct function is because I'll need to add this kind of filter to many pages individually, so creating Filters I'll need to create too many filters to verify if each page is enabled, is there any way to do this in a more "automatic"?

Since you don't want to use filter, you can try this.
  1. Create sub-BaseController controller extends BaseController and define your constructor and properties there. (E.g MyController or MasterController)
  2. Create your controller extends newly created sub-BaseController and create constructor with parent::__construct();

P/s: You can actually group the filter.
Reply
#5

(08-08-2021, 08:49 PM)nfaiz Wrote:
(08-08-2021, 03:18 PM)neto737 Wrote:
(08-08-2021, 01:18 AM)InsiteFX Wrote: Add a method in your BaseController to get the settings.
Example:
PHP Code:
protected function getSettings()
{
    return $this->settings;

Try that.

I tried to add this method in my BaseController, then did a var_dump in the another controller construct function but it returned null anyway.

I think the best workaround to this problem is using the CI4 Filters, but as I fetch all settings from database on BaseController, is there any way to use the data from this fetch instead of fetching it again on Filters?

The reason why I wanted to filter it on construct function is because I'll need to add this kind of filter to many pages individually, so creating Filters I'll need to create too many filters to verify if each page is enabled, is there any way to do this in a more "automatic"?

Since you don't want to use filter, you can try this.
  1. Create sub-BaseController controller extends BaseController and define your constructor and properties there. (E.g MyController or MasterController)
  2. Create your controller extends newly created sub-BaseController and create constructor with parent::__construct();

P/s: You can actually group the filter.

Actually using filter isn't a problem at all, I'm already using it (actually grouping routes as you suggested) to check if user is already authenticated, but I have a system that the admin will be able to enable/disable pages, it means that when an user access a disabled page he should be redirected back, so if I have 10 different pages that can be enabled/disabled I'll need to create 10 different filters.

The suggestion to create a sub-BaseController is valid, and I'll probably use it at some point to keep everything organized, but anyway it's not useful to my purpose as I can't redirect from the construct function.
Reply
#6

__construct is called before initController.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB