Welcome Guest, Not a member yet? Register   Sign In
default data passed through BaseController
#1

(This post was last modified: 03-29-2020, 06:07 AM by Codinglander.)

Hi there...

I'm trying to do my first project with CodeIgniter 4.

I want to pass standard data to View set inside the BaseController. 

But following code does not work:
Controller (BaseController)
PHP Code:
public $data = [
    'title' => 'defaultTitle',
   ];

/**
 * Constructor.
 */
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $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();

Users Controller:
PHP Code:
public function index()
    {
        $this->data['title'] = 'my own title';
        echo view('users/profile',$this->data);
    

I want to have default variables which I can change in the controller.

Any Ideas ?

Thanks,
Kigh...

Now it works fine, but I get an error with preloading "session":

Quote:Fatal error: Uncaught ErrorException: touch(): Unable to create file NULL/ci_session7roacrtom0dsiftqc3pdd0j1jnjv6ejo because No such file or directory in C:\xampp\htdocs\kighlander.local\system\Session\Handlers\FileHandler.php:252 Stack trace: #0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler(2, 'touch(): Unable...', 'C:\\xampp\\htdocs...', 252, Array) #1 C:\xampp\htdocs\kighlander.local\system\Session\Handlers\FileHandler.php(252): touch('NULL/ci_session...') #2 [internal function]: CodeIgniter\Session\Handlers\FileHandler->write('7roacrtom0dsift...', '__ci_last_regen...') #3 [internal function]: session_write_close() #4 {main} thrown in C:\xampp\htdocs\kighlander.local\system\Session\Handlers\FileHandler.php on line 252
Reply
#2

Your getting the session error because the variable session is missing in the BaseController.

Here is Mine.

PHP Code:
<?php
namespace App\Controllers;

/**
 * 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.
 *
 * @package CodeIgniter
 */

use CodeIgniter\Config\Services;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

class 
BaseController extends Controller
{

    
/**
     * 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 = [
        
'auth',
    ];

    
/**
     * @var string
     * Holds the session instance
     */
    
protected $session;

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

        // Ensure that the session is started and running
        
if (session_status() == PHP_SESSION_NONE)
        {
            
$this->session Services::session();
        }

    }


What did you Try? What did you Get? What did you Expect?

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

Ah, THAT was the issue...
Thank you very much !!!

<3
Reply




Theme © iAndrew 2016 - Forum software by © MyBB