Define global array constant for using in view

試著忘記壹切 提交于 2019-12-12 11:16:11

问题


I want to define global array constant

code in bootstrap.php

$adv_types = array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее');

code in view file

echo $form->input('Adv.type', array('type' => 'select', 'option' => $adv_types, 'label' => 'Место рекламы'));

but cakephp gives error:

"Undefined variable: adv_types"


回答1:


These need to 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);
        }
}

To me, bootstrap.php is not the correct file for this constant




回答2:


Unfortunately, the scope for bootstrap.php is bootstrap.php, so the $adv_types variable will be out-of-scope as soon as PHP completes parsing bootstrap.php.

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

Solution 1: you need this variables in many of your views

If you need the variable to be available in all views, you should define and set it in AppController::beforeRender().

In app/app_controller.php:

class AppController extends Controller
{

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

        $adv_types = array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее');
        $this->set(compact('adv_types'));
    }
}

This will allow any of your views to access the $adv_types array.

Solution 2: you might need access to this variable anywhere within your CakePHP app

If you must access the $adv_types variable elsewhere in your app, you can add it to the Configure collection. In bootstrap.php:

Configure::write('NameOfYourAppAsNamespace.adv_types', array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее'));

I recommend using the name of your project as a pseudo namespace; adv_types is unlikely to conflict with other identifiers, but if you start using this approach more often, your chances of creating conflicts increases. Additionally, this allows you to group the data you're storing in the Configure collection under one namespace, which can be handy for debugging.

Anyway, this approach will allow you to access the variable in any scope under the CakePHP umbrella, by invoking Configure::read(). Thus:

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

Solution 3: you absolutely must have this variable available as a global variable

If you absolutely must have this as a standard PHP global variable, you could do the following:

$GLOBALS['adv_types'] = array('top' => 'Верх', 'left' => 'Левое', 'right' => 'Правое', 'bottom' => 'Нижнее');

Before doing this, please consider whether this is strictly necessary. Global variables are a really messy business, and you should have a clear and present need to justify it.


Edit/Update!

Thirty seconds in Google Translate has led me to discover that your array contains translations corresponding to the keys. You might want to investigate using CakePHP's Internationalization & Localization features to abstract away the distinction between English and Russian terms for top/left/right/bottom (and everything else).



来源:https://stackoverflow.com/questions/4260067/define-global-array-constant-for-using-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!