-1

I'm migrating ZF1 application to ZF2, here's the code in controller which is using the action helper class UserBuilder:

$userBuilder = $this->userBuilder;

Currently the page complains:

Notice: Undefined property: Stock\Controller\ScreenerController::$userBuilder

From <Recommended Project Directory Structure>, we can see the directory application/controllers/helpers still exists.

I have lots of controller action helper in Zend Framework 1. I curious if there is some concept in Zend Framework 2? And how to migrate them?

user1633272
  • 2,007
  • 5
  • 25
  • 48
  • 1
    Maybe try to explain what you're actually wanting to do and we can help you. Are you talking about ControllerPlugins? If you're actually talking about the action helper in ZF1 for widgetized content, then this blog might interest you: [How to replace the "Action Helper" in ZF2 (and make great widgetized content)](http://www.michaelgallego.fr/blog/2012/10/06/how-to-replace-the-action-helper-in-zf-2-and-make-great-widgetized-content/) – Sam Apr 21 '13 at 16:36
  • I guess you have to declare the variable: **protected $userBuilder;** – Remi Thomas Apr 21 '13 at 18:35
  • Actually, UserBuilder is an action helper class written by me in ZF1. – user1633272 Apr 22 '13 at 03:50

2 Answers2

1

Long back ,I have viewed the Presentation of Enrico Zimuel at ZFCon 2012 of Moscow , in you tube , I am not able to find the link for that ,

But I could find his presentation at slide share , below is the link

ZF2 quick start

With in the presentation , Enrico Zimuel suggests a git repository which is hosting a sample application which works as an emulator .

Below is the link for that git repository which is actually a sample ZF1 application migrated to work with ZF2 . It works like an emulator to test ZF2 with ZF1 code.

ZF2 migration prototype

It is a ZF1 migration prototype

It actually

  1. Creates a "Zf1Compat" version of the ZF1 dispatcher as an event listener.
  2. The bootstrap largely mimics how ZF1's Zend_Application bootstrap works.
  3. The default route utilizes the new ZF2 MVC routing, but mimics what ZF1 provided.

I am posting this just to mention that there is a solution out there for solving the problem of migration which is really good.

This may also help others who needs to migrate, because no one knew that there is a git repository which has this prototype , and I have not seen any blog posting OR SO posting specifying this repository link any where ( I really don't know why ? ) .

NOTE : -

This only reduces your work of migration you will be loosing lots of advantages of ZF2 and also you can't really exploit the features of a framework .

Community
  • 1
  • 1
Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
0

Zend 1 action helpers correspond to Zend 2 controller plugins.

You just have to make just a few changes to make this work.

One way to create a controller plugin in Zend 2 is:

  • create a folder under Controller eg. "module/Application/src/Controller/Plugin"

  • Create a php file under the folder created with your plugin name eg. MyPlugin.php and add the following contents:

namespace Application\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class MyPlugin extends AbstractPlugin {

    public function doSomething()
    {
        // put your code here
        return '1';
    }

}
  • Add to your module.config.php(eg module/Application/config/module.config.php) the controller plugin:
    'controller_plugins' => array(
        'invokables' => array(
            'Plugin1' => 'Application\Controller\Plugin\MyPlugin',
        )
    ),
  • Use it in your controllers eg.
    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
            $plugin = $this->Plugin1(); // or $this->plugin('Plugin1');
            echo $plugin->doSomething();
            return new ViewModel();
        }
    }

Do not confuse Zend 1 plugins with Zend 2 controller plugins, these are different things.

Jannes Botis
  • 11,154
  • 3
  • 21
  • 39