繁体   English   中英

设置和访问变量错误页面ZF2

[英]Set & Access Variables in error Page ZF2

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');
}

类设置扩展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;
}

}

重播之后,我的Module.php文件是:

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;
                },
            ),
        );
    }
}
?>

在我的控制器(前端)中,我可以从数据库中获取数据并在布局中进行设置(此处为Setting_data)。

如果我的操作/方法很完美,我可以轻松获取集合数据索引视图页面。

但是我想在错误页面中设置数据也意味着如果我的操作/方法未找到页面。

我将如何获得这些数据?

我不想设置静态数据,我想调用动态数据并将其设置在一个变量中(此处为setting_data)。 我想在错误页面布局中访问此变量。

要么

好。 从我的对话中了解到(请参阅注释),您的$setting_data变量不取决于任何控制器环境,而仅取决于某些数据库数据。 在这种情况下,您需要的是我的第一条评论中的第一个链接。

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);
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM