1

I want to load a function from another controller. This is my structure:

- modules
--orderpages
---controllers
----WebshopCore.php
----WebshopController.php

My function insertItemInCart in WebshopController.php is called. But when i want to execute a function from another controller it crashes.

class WebshopController extends MX_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->module('orderPages/WebshopCore');
    }

    function insertItemInCart(){
        $partId = $this->input->post('partId');
        $quantity = $this->input->post('quantity');

        $output = $this->WebshopCore->getPickLocations($partId,$quantity);  

    }
}

My WebshopCore:

class WebshopCore extends MX_Controller {
    public function __construct() {
        parent::__construct();
    }

    public function getPickLocations($partId,$amount){
        $result = "test";

        return $result;
    }
}

What goes wrong? I don't get it

The solution:

$output = modules::load('orderPages/WebshopCore/')->getPickLocations($partId,$quantity);
da1lbi3
  • 4,369
  • 6
  • 31
  • 65
  • imo, debugging: `var_dump($this->WebshopCore, __FILE__.__LINE__);` immediately before `$output = ....`. Yopu should see the WebshopCore object. – Ryan Vincent Mar 05 '16 at 14:12
  • @RyanVincent NULL string(107) "/Applications/AMPPS/www/cart.local/application/modules/orderPages/controllers/WebshopController.php19" This is what i get back – da1lbi3 Mar 05 '16 at 14:16
  • @RyanVincent I did that already – da1lbi3 Mar 05 '16 at 14:24
  • You checked that `$this->load->module('orderPages/WebshopCore');` returns what you expect? It obviously doesn't as the call to the loaded module 'WebshopCore' fails? – Ryan Vincent Mar 05 '16 at 14:27
  • @RyanVincent How can i check what it returns? But I get no errors on that line – da1lbi3 Mar 05 '16 at 14:29
  • maybe interesting? [Use controller from another controller codeigniter](http://stackoverflow.com/questions/14947261/codeigniterhmvc-cross-module-call-controller-method). This is the end of my knowledge of 'CI'. – Ryan Vincent Mar 05 '16 at 14:38
  • @RyanVincent Thanks for you help!! It works now, the solution is in the question. Thanks! – da1lbi3 Mar 05 '16 at 14:51
  • 1
    Your welcome - glad it is working :) – Ryan Vincent Mar 05 '16 at 14:52
  • 1
    I'd suggest that you write a self-answer to your question (and even accept it after some days) and explain why it is working now, so other users may actually benefit from your question – Vickel Mar 05 '16 at 17:58
  • And your controller file name and class name should be like Webshopcore.php and `class Webshopcore extends MX_Controller {}` ucfirst for all file names and classes. –  Mar 05 '16 at 21:17
  • It is most of the time `modules::run('module/controller/function')` –  Mar 05 '16 at 21:17

1 Answers1

0

You should write a library in that case.

In application/libraries create Cart.php (or whatever you want)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart
{
  protected $ci;

    public function __construct()
    {
        $this->ci =& get_instance(); 
    }

    public function getPickLocations($partId, $qty)
    {
       //Your stuff
    }

}

And then in your controllers :

 $this->load->library("cart");
 $data = $this->cart->getPickLocations($this->input->post('partId'), $this->input->post('quantity'));
AdrienXL
  • 3,008
  • 3
  • 23
  • 39
  • Yes that is an option, but what i have read on this http://stackoverflow.com/questions/14947261/codeigniterhmvc-cross-module-call-controller-method is that the hmvc model can do this on a much shorter way. – da1lbi3 Mar 05 '16 at 14:26