Frage

i have a huge problem using the public variable "uses" in the CakePHP-Controller.

I set up my application with the following schema.

PagesController extends an AdminController which extends the AppController.

class PagesController extends AdminController 
class AdminController extends AppController

I do this because i also got an ApiController instead of the AdminController which used for (as you can mention) an additional API-Layer in my Application.

I include the additional-Layer by using this

 App::uses('AdminController', 'Controller');

in the PagesController and

App::uses('AppController', 'Controller');

in the AdminController.

It work fine for me this way.

EDIT:

class AppController extends Controller {

    public $uses = array('Print');

}

I want to make a Model called "Print" accessable in every of my Child-Controllers. That is why I initialize it in my AppController

class PagesController extends AdminController {

    public $uses = array('Article');

}

In PagesController I want to use "Print"-Model and in addition that declared "Article"-Model. In the Commentary for that $uses-variable is written down that every addition in the Child-Controller will merged into the parent-$uses-variable. But it doesnt do. it overwrite it.

Where is the mistake? Please Help.

War es hilfreich?

Lösung

And it works ok:

AppController.php

App::uses('Controller', 'Controller');
class AppController extends Controller {
    public $uses = array('Print');
    public function test() {
        echo 'AppController => ';
    }
}

AdminController.php

App::uses('AppController', 'Controller');
class AdminController extends AppController {
    public $uses = array('Photo');
    public function test() {
        parent::test();
        echo 'AdminController';
        if (is_subclass_of('AdminController', 'AppController')) {
            echo ' (Subclass of AppController)';
        }
        echo ' => ';
    }
}

PagesController.php

App::uses('AdminController', 'Controller');
class PagesController extends AdminController {
    public $uses = array('Article');
    public function test() {
        parent::test();
        echo 'PagesController';
        if (is_subclass_of('PagesController', 'AdminController')) {
            echo ' (Subclass of AdminController)';
        }
        if (is_subclass_of('PagesController', 'AppController')) {
            echo ' (Subclass of AppController)';
        }
    }
    public function index() {
        self::test();
        pr($this->uses);
        exit;
    }
}

/pages/index

AppController => AdminController (Subclass of AppController) => PagesController (Subclass of AdminController) (Subclass of AppController)
Array
(
    [0] => Article
    [1] => Print
)

Cake merges only with one controller (AppController by default), not all the way up (AdminController->AppController->Controller). That's why there is no Photo model in Pages.

Hope this helps.

EDIT: You can try to merge $uses from Pages, Admin, App (and any class between) like this: AppController.php

protected function _mergeControllerVars() {
        parent::_mergeControllerVars();
        $parent = get_parent_class($this);
        while ($parent != 'Controller') {
            $parentVars = get_class_vars($parent);
            if (isset($parentVars['uses']) && $parentVars['uses'] !== true) {
                $this->uses = array_merge(
                    $this->uses,
                    array_diff($parentVars['uses'], $this->uses)
                );
            }
            $parent = get_parent_class($parent);
        }
    }

Andere Tipps

I think you have syntax problem, can you try below solution. Please write following code in your controller:

public var  $uses = array('Print');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top