设置 &访问错误页面 ZF2 中的变量

编程入门 行业动态 更新时间:2024-10-28 12:19:46
本文介绍了设置 &访问错误页面 ZF2 中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
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 {

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

}

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

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

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

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

我不想设置静态数据我想调用动态数据并将其设置在一个变量中(这里是setting_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.

哪个控制器调用错误页面?

推荐答案

好的.正如我从我们的谈话中了解到的(见评论),你的 $setting_data 变量不依赖于任何控制器环境,而只依赖于一些数据库数据.在这种情况下,我第一条评论中的第一个链接就是您所需要的.

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

这篇关于设置 &访问错误页面 ZF2 中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 13:42:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1042349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   错误   页面   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!