2

I am using a Plugin for CakePHP - AjaxMultiUpload (a fine plugin :-) - but I have a question about how to change/access a variable that is defined in the Controller that is part of the Plugin.

I have used this plugin before and like it. But I am trying to restrict what file types are allowed to be uploaded based on which controller I have attached the Component to.

In the Plugin there is a Controller - UploadsController.php. In this class, there is a public variable:

// list of valid extensions, ex. array("jpeg", "xml", "bmp")
public $allowedExtensions = array();

If I change this to:

public $allowedExtensions = array('mp3');

when I try to upload a .jpg file, an error message is issued (as one would hope :-).

In my controller (Tracks) I have attached the AjaxMultiUpload Plugin. It works. In the beforeFilter() method in my Tracks controller, I have placed the following line:

$this->Upload->allowedExtensions = array('mp3');

This had no effect. (Incidentally, there are no warnings thrown telling me I have tried to access something that doesn't exist).

I don't want to change the contents of the array allowedExtensions by hard-coding it in the Plugin's controller. This is bad for 2 reasons: 1) updatability of the Plugin and 2) I want to change the restrictions (audio file types versus image file types, for example) based on which of my controllers (Tracks, Images, etc.) the Plugin is attached to.

What I don't understand is the relationship between the Component (attached to the Tracks controller) and the UploadsController that is part of the Plugin. What instantiates the UploadsController that is part of the Plugin? Does this happen automagically?

Can anyone help?

Thanks, Ken

0

3 Answers 3

1

You can get the Controller of Component in CakePhp 3.x as below:

$objController = $this->_registry->getController();

Sign up to request clarification or add additional context in comments.

Comments

1

If you had a variable in your Controller called $fooVariable, you would access it in an associated component as follows: $this->getController()->myVariable.

Note that the variable need to be public in the controller. If the variable is protected or private, you may inject it in the component while loading it.

1 Comment

Please review How do I write a good answer. Code-only answers are discouraged because they do not explain how they resolve the issue in the question. You should update your answer to explain what this does and how to use it.
0

Components are accessed the same was as Models in Cake from a controller. I've created a Test component and included it in both a controller from the main application, and a controller from a plugin:

app/controllers/components/test.php

class TestComponent extends Object {
    public $allowedExtensions = array(); // put your defaults in the component
}

app/controllers/my_controller.php

class MyController extends AppController {
    // ... vars
    var $components = array('Test', /* ...others */);
    var $layout = '';

    public function test() {
        $this->render(false);

        // get current values from component
        debug($this->Test->allowedExtensions);

        // add a JPG
        $this->Test->allowedExtensions[] = 'jpg';
        debug($this->Test->allowedExtensions);

        // add a MP3
        $this->Test->allowedExtensions[] = 'mp3';
        debug($this->Test->allowedExtensions);
    }
}

Output from this test is:

app\controllers\my_controller.php (line n) Array ( )

app\controllers\my_controller.php (line n) Array ( [0] => jpg )

app\controllers\my_controller.php (line n) Array ( [0] => jpg [1] => mp3 )

So from this you can see that the public property of the Test component is available from any controller that uses it.

Using the exact same method to include the component from the main project (plugin specific models, components etc need to be prefixed with the plugin's name) produces the same results, you can in fact control that component property from a plugin or any controller individually and still have your default values in the component it self.

1 Comment

I know how to access a Component. That was not my question. The class variable allowedExtensions is NOT part of the component class. It is declared in a Controller that is part of the Plugin. I don't know how that Controller is getting instantiated and in what context can I modify it's value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.