Set & Access Variables in error Page ZF2

2019-02-20 10:39发布

class FrontController extends AbstractActionController {

    public function indexAction() {
        $this->layout()->setting_data = $this->getSetting()->getSettingContent(1);
        return array();
    }

}

public function getSetting() {
    return $this->getServiceLocator()->get('Front/Model/Setting');
}

class Setting extends AbstractTableGateway {

public function __construct($adapter) {
    $this->table = 'setting';
    $this->adapter = $adapter;
}
public function fetchAll() {
    return $this->select();
}
public function getSettingContent($id){
    $id  = (int) $id;
    $rowset = $this->select(array('id'=>$id));
    if (!$row = $rowset->current()){
        throw new \Exception ('Row not found');
    }
    return $row;
}

}

My Module.php file is after your replay is:

namespace Front;
/*use Zend\ModuleManager\ModuleManager;*/
class Module
{

public function onBootstrap($e)
{
    $event = $e->getApplication()->getEventManager();

    $event->attach('render', function($e) {
        $serviceManager = $e->getApplication()->getServiceManager();
        $dbadapter = $serviceManager->get('Zend\Db\Adapter');

        /*
         * Through this adapter make sql-request and
         * fetch data that you need and give it to
         * $setting_data variable
         */

        $e->getViewModel()->setVariable('setting_data', $setting_data);
    });
}
    public function getAutoloaderConfig()
    {
        return array('Zend\Loader\StandardAutoloader' =>
            array('namespaces' =>
                array(__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,),
            ),
        );
    }
/*    public function init(ModuleManager $moduleManager)
    {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            // This event will only be fired when an ActionController under the MyModule namespace is dispatched.
            $controller = $e->getTarget();
            $controller->layout('layout/frontlayout');
        }, 100);
    }
*/
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    // Add this method:
    public function getServiceConfig()
    {
        return array(
        'Zend\Db\Adapter' => function($sm){
            $global_config = $sm->get('Configuration');
            $db_params = $global_config['db'];
            return new Adapter($db_params);
        },
            'factories' => array(
                'Front\Model\AlbumTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\AlbumTable($dbAdapter);
                    return $table;
                },
                'Front\Model\Cms' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Cms($dbAdapter);
                    return $table;
                },
                'Front\Model\Setting' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Setting($dbAdapter);
                    return $table;
                },
                'Front\Model\Slider' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Slider($dbAdapter);
                    return $table;
                },
                'Front\Model\Schedule' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new \Front\Model\Schedule($dbAdapter);
                    return $table;
                },
            ),
        );
    }
}
?>

In my controller(Front) I can fetch data from database and set in layout.(Here Setting_data).

I can easily fetch set data index view page if my action/method is perfect.

but i want to this set data in error page also means if my action/method not found page.

How can i will get this data.?

I don't want to set static data I want to call dynamic data and set it in one variable(Here setting_data). I want to access this variable in my error page layout.

OR

Which controller call for error page?

1条回答
该账号已被封号
2楼-- · 2019-02-20 11:15

Ok. As I understood from our conversation (see comments), your $setting_data variable not depends on any controller environment but only on some database data. In that case the first link from my first comment is what you need.

public function onBootstrap($e)
{
    $event = $e->getApplication()->getEventManager();

    $event->attach('render', function($e) {
        $serviceManager = $e->getApplication()->getServiceManager();

        $setting = $serviceManager->get('Front/Model/Setting');
        $setting_data = $setting->getSettingContent(1);

        $e->getViewModel()->setVariable('setting_data', $setting_data);
    });
}
查看更多
登录 后发表回答