1

I am building a component which will act as a API for a front end and was curious about something. For example if I call the url http://example.com/index.php?option=com_helloworld&task=shout it will work without any problem. This is all in my components main controller.php file. However I want to add a constructor to define class variables to use throughout my various functions (tasks). If I then try to run a task, it will give me a 404 error saying the task can't be found. If I remove the constructor it works again.

So I guess my question is can I run a task with a constructor in the controller.php file or is it because I am targeting a specific task it won't run, which seems odd to me.

Edit

I want to have a class variable and set that to a model I will be using

eg

class myController extend JController {
public $studentModel;
public function __construct($config = array()) {
    $this->studentsModel = $this->getModel('students');
    parent::__construct($config);
  }
}

Can this be done? I will be using the model in multiple tasks. I know its only 1 line but it would be helpful. I have tried but I can't access any of the functions in the model.

  • The controller.php does work with constructors. Can you show us the full code for your controller? – Lodder Jul 12 '18 at 8:13
  • Hi I realised I had to add parent::__construct($config); to my constructor and it seems to be working now. – Alex Mehan Jul 17 '18 at 0:24
  • Actually I do have a follow up question - will update the question. – Alex Mehan Jul 17 '18 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.