Welcome Guest, Not a member yet? Register   Sign In
Loading helper before controller
#1

(This post was last modified: 08-27-2020, 11:04 AM by Cyto5.)

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.
Reply
#2

Can you show some code?
Reply
#3

(This post was last modified: 08-27-2020, 01:28 PM by Cyto5.)

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();
        ...
    }

Reply
#4

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

Reply
#5

(This post was last modified: 08-27-2020, 01:37 PM by Cyto5.)

(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');
    }


Reply
#6

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

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

(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!
Reply
#8

You could also place it in common.php
What did you Try? What did you Get? What did you Expect?

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

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();    
         
Reply
#10

I think what you want there are controller filters.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB