Welcome Guest, Not a member yet? Register   Sign In
Using Preloaded Model Via BaseController in Other Controllers
#3

Hi, more code.

The intent is to have a model for each controller if really need specific data for that page.

The base layout view will be driven by the 'base layout markup' model, where the 'controller specific' models will call its '_base_layout_markup' function.


My BaseController:

PHP Code:
namespace App\Controllers;

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 CLIRequest|IncomingRequest
   */
  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 = [
  'Common_functions'
];

  /**
   * Constructor.
   */
  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();

    // The default DB connection.
    $this->db = \Config\Database::connect();

    // The site's layout view markup model, available to all controllers which
    // is extended from this BaseController.  Shared instance of the model.
    $this->base_layout_markup_mdl model'App\Models\Base_layout_markup_model' );
  

The site home controller:

PHP Code:
namespace App\Controllers;

/**
* Class Site_home
*
* Site_home is the class for the site's home page.
*/
class Site_home extends BaseController
{
  /**
* The Site Home Model

* @var Object
*/
private $site_home_mdl;

/**
* The Constructor.
*/
public function __construct()
{
// Shared instance of the model.
$this->site_home_mdl model'App\Models\Site_home_model' );
}

/**
* Site Home Index page.

* @param NIL

* @return PageView
*/
  public function index()
  {
    // Obtain Site Home Index Data.
    $data $this->site_home_mdl->_index();


    // Return the view.
    return view'site/site_index_body'$data );
  

The site home model:

PHP Code:
namespace App\Models;


/**
 * Site Home model
 * 
 * The Site_home_model obtains the data for the site's home page.
 */
class Site_home_model extends Base_layout_markup_model
{
 
/**
 * The Constructor.
 */
 
public function __construct()
 {
 
 }

 
/**
 * Site Home Index page.
 * 
 * Obtain and return an array of date for site home index page.
 * @param NIL
 * 
 * @return array
 */
  public function _index() : array
  {
    $base_markup $this->_base_layout_markup();

    $data['base_layout_markup'] = $base_markup;


    return $data;
  

The base layout markup model:

PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;

/**
 * Base layout markup info model
 * 
 * The Base_layout_markup_info_model has functions which obtain the site's
 * layout view markup.
 */
class Base_layout_markup_model extends Model
{
 protected 
$table      'page_markup_info';
  protected $primaryKey 'page_markup_info_id';

  /**
  * Instance of the main Request object.
  *
  * @var CLIRequest|IncomingRequest
  */
  private $_request;

 
/**
 * The requested URI Object.
 * 
 * @var Object
 */
 
private $_uri_obj;

 
/**
 * The number of URI segments.
 * 
 * @var Integer
 */
 
private $_nos_uri_segments;

 
/**
 * The requested URI cast to a string.
 * 
 * @var String
 */
 
private $_uri_str;

 
/**
 * Segment One of the URI.
 * 
 * @var String
 */
 
private $_segment_one;

 
/**
 * Segment Two of the URI, defaults to NULL.
 * 
 * @var String
 */
 
private $_segment_two NULL;

 
/**
 * Segment Three of the URI, defaults to NULL.
 * 
 * @var String
 */
 
private $_segment_three NULL;

 
/**
 * The Constructor.
 */
 
public function __construct()
 {

 }

 
/**
 * Base Layout Markup.
 * 
 * Obtain and return an array of markup for the base layout view.
 * @param NIL
 * 
 * @return array
 */
 
public function _base_layout_markup$request ) : array
  {
 
// The request.
 
$this->_request $request;

 
// The URI Object.
 
$this->_uri_obj $this->_request->uri;

 
// The number of URI segments.
 
$this->_nos_uri_segments $this->_uri_obj->getTotalSegments();

 
// The URI, cast to a string.
 
$this->_uri_str = (string)$this->_request->uri

I am now at the point where I receive Attempt to read property "uri" on null on the following line:

Code:
$this->_uri_obj = $this->_request->uri;

The only way around this is by replacing:

Code:
$this->_request = $request;
with the following and removing the passed in $request variable:
Code:
$this->_request = \Config\Services::request();

Is that considered a 'best practice'?
Reply


Messages In This Thread
RE: Using Preloaded Model Via BaseController in Other Controllers - by Mr Lister - 10-10-2021, 11:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB