简体   繁体   中英

ZF2 - Where should i put my include files?

im still learning ZendFramework2, and I want to know where I should put my include files in zf2.

I started to migrate my old PHP system to zf2, and all was going well and until I noticed that I have some PHP files that return arrays, and are used to convert ID in description. For example:

item.db.php

return array(  
     441 => "Some description",  
     442 => "another description"   
); 

Due to the large amount of data in the files, I don't want to put them in a single method. I'm going to use the files as a view helpers.

There are multiple ways available in zend framework like. If your array size is small and is related to your tables then you can keep that in model file.

public static function myarray(){
    return array(  
     441 => "Some description",  
     442 => "another description"   
    );
}

and youn can access like this. Mymodule\\Mymodel::myarray();

else you can create view helper and you can access them anywhere. eg:

Create a sample class MyHelper in view/helper

namespace MyModule\View\Helper;    
use Zend\View\Helper\AbstractHelper;

class MyHelper extends AbstractHelper
{       
    public function __invoke(){
        return $this;
    }

    public function render($arg1, $arg2){
        return 'My Expected output ' . $arg1 . ' ' . $arg2;
    }

    public function generateInfo(){
        return 'Your HTML goes here';
    }
}

Now register this class in module.config.php so that you can access this in views.

'view_helpers' => array(
    'invokables' => array(
        'myHelper' => 'MyModule\View\Helper\MyHelper',
    ),
),  

and call like this

<?php echo $this->myHelper()->generateInfo(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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