Magento - 在前端和后端之间传递数据

This is my first time with Magento. I have to prepare module which adds select field (yes/no) into General information (Category in admin Panel). I have already done this part. Next step is to check value selected in General information form when user goes to category side. If the user is not logged-in and admin option has been selected to "yes" in General information form, system should display information like: "you must log in".

Below my folder structure:

- app
 -> code
 -> community
 -> AttributeCategory
 ->CustomAttributeCategory->
 - etc
    -> config.xml 

<?xml version="1.0"?>
<config>
    <modules>
        <AttributeCategory_CustomAttributeCategory>
            <version>0.0.3</version>
        </AttributeCategory_CustomAttributeCategory>
    </modules>

    <global>
        <resources>
            <add_category_attribute_login>
                <setup>
                    <module>AttributeCategory_CustomAttributeCategory</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </add_category_attribute_login>
            <add_category_attribute_login_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </add_category_attribute_login_write>
            <add_category_attribute_login_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </add_category_attribute_login_read>
        </resources>
    </global>
</config>

 - sql -> add_category_attribute_login ->
 - mysql4-install-0.0.3.php :


<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'is_category_allowed', [
    'group'      => 'General Information',
    'type'       => 'int',
    'input'      => 'select',
    'label'      => 'required logged-in user',
    'sort_order' => 1000,
    'visible'    => true,
    'required'   => true,
    'source' => 'eav/entity_attribute_source_boolean',
    'visible_on_front' => true,
    'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'option'     => [
        'values' => [
            0 => 'No',
            1 => 'Yes',
        ]
    ],
]);
$this->endSetup();

AND 

- app->etc->modules:
AttributeCategory_CustomAttributeCategory.xml:

    <?xml version="1.0"?>
<config>
    <modules>
        <AttributeCategory_CustomAttributeCategory>
            <active>true</active>
            <codePool>community</codePool>
        </AttributeCategory_CustomAttributeCategory>
    </modules>
</config>

Please, tell me how can I check value in front when users visit category pages?

You should create an observer which looks at the attribute value on the category that is loaded, then performs the check and if necessary sets an error message and redirects to the customer's login page.

You could observe the event catalog_controller_category_init_after which is dispatched in Mage_Catalog_CategoryController::_initCategory after the category is loaded. This means that all attributes for the category will be available to look at, regardless of whether they are in the category flat tables or not.

Create an observer:

// File: app/code/community/AttributeCategory/CustomAttributeCategory/Model/Observer.php
class AttributeCategory_CustomAttributeCategory_Model_Observer
{
    public function checkLoggedInForCategory(Varien_Event_Observer $event)
    {
        // Get the category from the event
        /** @var Mage_Catalog_Model_Category $category */
        $category = $event->getCategory();

        // Get the customer's session model
        /** @var Mage_Customer_Model_Session $customerSession */
        $customerSession = Mage::getSingleton('customer/session');

        if ($category->getIsCategoryAllowed() && !$customerSession->isLoggedIn()) {
            // Add a custom message here?
            $customerSession->addError('You must be logged in to view this category.');

            // Redirect to login page
            Mage::app()
                ->getResponse()
                ->setRedirect(Mage::getUrl('customer/account/login'))
                ->sendResponse();
            exit;
        }
    }
}

The logic here basically says "get the category from the event" which can be done because the point where it is dispatched passes it as an argument, "get the customer session" which is always available regardless of whether the customer is logged in or not, "check 'is_category_allowed' is truthy and that the customer is not logged in" and if so add a validation error message, and redirect to the login page.

The login page automatically renders and displays all message block entries, so you don't need to handle the display manually.

Now you need to define your observer in your config.xml and connect it to the event:

<!-- File: app/code/community/AttributeCategory/CustomAttributeCategory/etc/config.xml -->
<?xml version="1.0"?>
<config>
    <modules>
        <AttributeCategory_CustomAttributeCategory>
            <version>0.0.3</version>
        </AttributeCategory_CustomAttributeCategory>
    </modules>

    <global>
        ...
    </global>
    <frontend>
        <events>
            <catalog_controller_category_init_after>
                <observers>
                    <ensure_customer_can_view_category>
                        <class>AttributeCategory_CustomAttributeCategory_Model_Observer</class>
                        <method>checkLoggedInForCategory</method>
                    </ensure_customer_can_view_category>
                </observers>
            </catalog_controller_category_init_after>
        </events>
    </frontend>
</config>

I hope this helps. There's plenty of resource online about how to create observers etc, they're very useful things to use. This registers an observer in the class AttributeCategory_CustomAttributeCategory_Model_Observer, method named checkLoggedInForCategory which is connected to the catalog_controller_category_init_after event in the frontend only. You can always define this in the global scope as well, but there's no point since it's only dispatched in the frontend, and should only be used for customers in the frontend.