0

In views\scripts I have header.phtml file, there is placed infomation about user: Name, Surname, photo

Now I must in each controller constructor get this data from db, maybe is better solution to use abstract class or something else ?

Because it hard to place same code in each controller constructor.

3 Answers 3

2

Yes there are two recommended solutions, first one is to make a plugin (http://framework.zend.com/manual/1.12/en/zend.controller.plugins.html) that gets loaded for example in your bootstrap and will do something for each request, if what you want to do applies only to a few actions you could create an action helper (http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html).

In your case i would create a frontController plugin for example on dispatchLoopStartup that stores the user data in the registry, then in your view you retrieve that data from the registry and echo it. To reduce the amount of db requests, the plugin could use the user session to store the informations, so only the first request to your site would trigger a db query, or put the user data in the session upon login.

I dont recommend using a baseController as it is not a best practice and you would have to edit all your controllers to tell them to extend the baseController, i will also not use the init function of all your controllers for the same reason, by using the plugin all you need to do is initialize it in your boostrap and then retrieve the data from registry in your header view partial

the name of the plugin starts with the name of your library directory, followed by any subfolders that you may have and after the last underscore is the name of your class, this is why the autoloader finds your plugin by replacing the underscores with slahes or backslashes depending on your OS to get the correct path

header.phtml:

<?php 
// test if userData is there
Zend_Debug::dump($userData);

Bootstrap.php:

<?php

// add the namespace of your library directory
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLibrary_');
// register header plugin
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new headerUserDataPlugin());

Put the following plugin into the correct folder and adapt the plugin class name if your directories structure differs from this one, for example your Library folder is maybe not called "myLibrary":

application/
        configs/
            application.ini
        forms/
        layouts/
        modules/
        views/
        Bootstrap.php
    library/
        Zend/
        MyLibrary/
            Controller/
                Plugin/
                    HeaderUserDataPlugin.php
    public/
        index.php

HeaderUserDataPlugin.php:

<?php

/**
 * header user data plugin
 *
 **/
class MyLibrary_Controller_Plugin_HeaderUserDataPlugin extends Zend_Controller_Plugin_Abstract {

    public function routeShutdown(Zend_Controller_Request_Abstract $request) {

        // at this point you can retrieve the user id from session
        $mysession = new Zend_Session_Namespace('mysession');

        // is there a userId in the session
        if (!isset($mysession->userId)) {

            $userId = $mysession->userId;

            // get userData from user model
            $userModel = new UserModel();
            $userData = UserModel->getUserData($mysession->userId);

            Zend_Registry::set('user_data', $userData);

        }

    }

}

I did not test the code, there may be some typos :S

Sign up to request clarification or add additional context in comments.

2 Comments

I get error Class headerUserDataPlugin not found in C:\wamp\www\application\Bootstrap.php
yeah it probably it does not find the plugin, i updated my answer, i have added two things, two lines to the bootstrap to register a namespace for your own Library, in my case my library has a subdirectorie called "Controller" in which is another subdirectory called "Plugin", i have added the plugin into that directory, finally i have changed the plugin className and its corresponding fileName accordingly to my directory structure. Adapt both things in your own code, the changes must reflect your own folder structure.
2

Define a BaseController, put the things each controller needs to do in there and extend it by your regular controllers...

Comments

1

If I understood this question correctly, you want to call DB with same query in each controller construct, assign view variables with returned result and print them in header.phtml. If that's what you want, read along.

If memory is not an issue, the easier way to do this is to store a json encoded array with required data in a registry with Zend_Registry in bootstrap. This way, you only need to make only 1 query to DB.

Bootstrap.php

$row = $db->fetchRow("SELECT name, surname, url FROM people WHERE id = '127'");
$userData = array('name' => $row ['name'], 'surname' => $row ['surname'], 'photo' => $row ['url']);
Zend_Registry::set('user_data', json_encode($userData));

Now, you dont need to put this code in any controller. Just make a view helper which reads from Zend_Registry and returns required property. You can call it from your header.phtml directly.

/views/helpers/User.php

class Zend_View_Helper_User
{
    public function user($property)
    {
        $userData = Zend_Registry::get('user_data');
        $userData = json_decode($userData, true);
        return $userData[$property];
    }
}

header.phtml

<p><?=$this->user('name')?> <?=$this->user('surname')?></p>
<img src="<?=$this->user('photo')?>"/>

3 Comments

But is one problem in Bootstrap.php I not have user_id, I get user_id from session data
there is already everything that you need in my answer ;) ... dont put sql logic into the plugin, put the code in the model and call the model inside of the plugin, the view helper is a good idea
"plugin" you mean Bootstrap.php ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.