Defining a global array constant for use in the field of view

I want to define a global array constant

code in bootstrap.php

$adv_types = array('top' => '', 'left' => '', 'right' => '', 'bottom' => '');

code in file

echo $form->input('Adv.type', array('type' => 'select', 'option' => $adv_types, 'label' => ' '));

but cakephp gives an error:

"Undefined variable: adv_types"

+5
source share
2 answers

They should be set in your app_controller.php and then passed to your views

// app_controller.php
class AppController extends Controller {
        var $adv_types = array('top' => '', 'left' => '', 'right' => '', 'bottom' => '');
        function beforeFilter() {
            $this->set('adv_types', $this->adv_types);
        }
}

For me, bootstrap.php is not the right file for this constant

+5
source

Unfortunately, the scope bootstrap.phpis equal bootstrap.php, so the variable $adv_typeswill not be available as soon as PHP completes the parsing bootstrap.php.

There are several approaches you can take, depending on your actual requirements.

1:

, , AppController::beforeRender().

app/app_controller.php:

class AppController extends Controller
{

    function beforeRender()
    {
        parent::beforeRender();

        $adv_types = array('top' => '', 'left' => '', 'right' => '', 'bottom' => '');
        $this->set(compact('adv_types'));
    }
}

$adv_types.

2: CakePHP

$adv_types , Configure. bootstrap.php:

Configure::write('NameOfYourAppAsNamespace.adv_types', array('top' => '', 'left' => '', 'right' => '', 'bottom' => ''));

; adv_types , , . , , Configure , .

, CakePHP, Configure::read(). :

$adv_types = Configure::read('NameOfYourAppAsNamespace.adv_types');

3: ,

PHP, :

$GLOBALS['adv_types'] = array('top' => '', 'left' => '', 'right' => '', 'bottom' => '');

, , , . - , .


Edit/Update!

30 Google Translate , , . CakePHP , /// ( ).

+8

All Articles