CodeIgniter Forums
Loading helper before controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Loading helper before controller (/showthread.php?tid=77409)

Pages: 1 2


Loading helper before controller - Cyto5 - 08-27-2020

What is a good way to load a helper before the controllers run. I am trying to use a function in a helper file inside my controller __construct() function and I am autoloading them in the basecontroller right now but they are not available when __construct() is ran. What would be the best method of making sure the helper is available from within a controllers __construct()?

Thanks.


RE: Loading helper before controller - paulbalandan - 08-27-2020

Can you show some code?


RE: Loading helper before controller - Cyto5 - 08-27-2020

I want this but my Perm class uses a function in an auto-loaded helper file so this doesn't work.

PHP Code:
class AdminBaseController extends \App\Controllers\BaseController
{
    public function 
_defaultAdminPerms()
    {
        Auth::reqLogin();
        Perm::hasOrRedirect('IS_ADMIN''/dashboard');
    }
}


class 
Courses extends \App\Controllers\Admin\AdminBaseController
{
    public function 
__construct()
    {
        
parent::__construct();
        
$this->_defaultAdminPerms();
    }



However this works but it requires calling the function at the start every every public controller

PHP Code:
class AdminBaseController extends \App\Controllers\BaseController
{
    public function 
_defaultAdminPerms()
    {
        Auth::reqLogin();
        Perm::hasOrRedirect('IS_ADMIN''/dashboard');
    }
}


class 
Courses extends \App\Controllers\Admin\AdminBaseController
{
    public function 
__construct()
    {
        
parent::__construct();
    }
    
    public function 
f1()
    {
        
$this->_defaultAdminPerms();
        ...
    }
    
    public function 
f2()
    {
        
$this->_defaultAdminPerms();
        ...
    }
    
    public function 
f3()
    {
        
$this->_defaultAdminPerms();
        ...
    }




RE: Loading helper before controller - jreklund - 08-27-2020

Don't know exactly why this is (haven't looked in the code itself), but you can use an initController method instead of __construct to do what you want.

PHP Code:
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);

    
$this->_defaultAdminPerms();




RE: Loading helper before controller - Cyto5 - 08-27-2020

(08-27-2020, 01:27 PM)jreklund Wrote: Don't know exactly why this is (haven't looked in the code itself), but you can use an initController method instead of __construct to do what you want.

PHP Code:
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);

    
$this->_defaultAdminPerms();

Thank you I think that is working now I actually have it inside a custom admin adminbasecontroller that extends the default basecontroller and it looks to be working from that.

PHP Code:
class AdminBaseController extends \App\Controllers\BaseController
{
    public function 
__construct()
    {
        
parent::__construct();
    }
    
    public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
parent::initController($request$response$logger);
        
$this->_defaultAdminPerms();
    }
    
    public function 
_defaultAdminPerms()
    {
        Auth::reqLogin();
        Perm::hasOrRedirect('IS_ADMIN''/dashboard');
    }





RE: Loading helper before controller - jreklund - 08-27-2020

PHP Code:
public function __construct()
    {
        
parent::__construct();
    } 

This code are unnecessary, in case you are only doing just that.


RE: Loading helper before controller - Cyto5 - 08-27-2020

(08-27-2020, 02:49 PM)jreklund Wrote:
PHP Code:
    public function __construct()
    {
        parent::__construct();
    

This code are unnecessary, in case you are only doing just that.

I have other code inside that function but good tip if anyone else does that and has nothing else. Thanks!


RE: Loading helper before controller - InsiteFX - 08-28-2020

You could also place it in common.php


RE: Loading helper before controller - someone - 10-01-2020

Is this a good way to control before and after actions in CI4?
PHP Code:
   private function beforeRequest()
    {
        echo "Before";
    }

    private function afterRequest()
    {
        echo "After";
    

And my initController looks like that;


PHP Code:
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
                $this
->beforeRequest();
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

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



RE: Loading helper before controller - InsiteFX - 10-01-2020

I think what you want there are controller filters.