开发者

Define global array constant for using in view

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"


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).


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜