Migration issue from ZF2 to ZF3 - php

I am working on a project to migrate from ZF2 to ZF3. I am facing a problem and try to solve by googled but no luck.
$this->dbAdapter = $this->getServiceLocator()->get('db');
$DBQueryObj = new DBQuery($this->dbAdapter, 'tbl_user'); // custom function
I need a similar code in ZF3 for this line so that I can pass adapter instance in my custom function.
$this->dbAdapter = $this->getServiceLocator()->get('db');
Can anyone point me how to achieve this in ZF3?
Thank you

service names are case sensitive
fore example :
zf2 there was "viewhelpermanager" as well as "ViewHelperManager", now in Zf3, only "ViewHelperManager" available.
but in Zf3 add bellow line in Module.php.
public function getServiceConfig()
{
return array(
'aliases' => array(
'viewhelpermanager' =>'ViewHelperManager'
)
);
}
If getServiceConfig() exists then add
'aliases' => array(
'viewhelpermanager' =>'ViewHelperManager'
)
for more infomation and details, please see migration guide Zf2 to Zf3

Related

ZF2 view plugin manager not merging config

On my module.config.php I've got something like:
return [
'view_helpers' => [
'invokables' => [
'mycustomviewhelper' => 'Namespace\View\Helper\MyCustomViewHelper',
],
],
];
I have also got a utility class that will handle the responsibility of rendering a helper. Something like Zend\Paginator.
Zend\Paginator has a __toString() method that proxies to render() call, which instantiates View\Renderer\PhpRenderer() and then calls $view->paginationControl($this).
I am trying to replicate the similar functionality in my utility class, which has similar strategy to what Zend\Paginator already does, the only thing being different is my view helper is a custom one. Hence, my code looks like:
$view->MyCustomViewHelper($this);
This does not work, because the PhpRenderer ignores the config defined manually and does the following in getHelperPluginManager:
$this->setHelperPluginManager(new HelperPluginManager());
I've tried invoking the helpers already defined in ViewHelperManager and this works well.
I did try merging in the config beforehand and then setting the PhpRenderer in the view but then this caused other problems, such as my partials were not found etc.
Now my question is why does ZF not consider any custom registered views when trying to render it in isolation. Is there any other way to do this?
Thank you.
Right, after a bit of a debugging, and playing with the configs, I was able to make this work. Still not sure if this is the best way to do this, but looks like currently there's no other way to make it work.
I created a factory for the utility class, instantiated the PhpRenderer, and then merged in my config with the ViewPluginManager manually. My factory now looks like:
public function createService(ServiceLocatorInterface $serviceLocatorInterface)
{
$dataTable = new DataTable;
$phpRenderer = new PhpRenderer();
$config = new \Zend\ServiceManager\Config([
'invokables' => [
'datatablerenderer' => 'DataTable\View\Helper\DataTableRenderer'
],
]);
$phpRenderer->setHelperPluginManager(new HelperPluginManager($config));
$dataTable->setView($phpRenderer);
return $dataTable;
}
However will have to refactor this so that the config comes from the view_helpers config key and is not hardcoded.

Zend Framework 2 - Setting serviceLocator in HydratorPluginManager not working

I am using a HydratorPluginManager (Zend\Stdlib\Hydrator\HydratorPluginManager) to manage my hydrators in one single spot and to let the manager take care of validation of my hydrators (meaning checking if my hydrators properly implement the HydratorInterface).
I have some hydrators that I created using factories like this:
'service_manager' => array(
'factories' => array(
'My\Hydrators\SomeHydrator' => 'My\Hydrators\SomeHydratorFactory'
)
)
After registering the factories in my config file these hydrators are without any problem available in my ServiceManager using $serviceManager->get($name). Now I would like to connect my ServiceManager to my HydratorManager so that if I ask for a certain hydrator using:
$hydratorPluginManager->get($name)
where $name is the alias used for registering. So in this example it would be:
$hydratorPluginManager->get('My\Hydrators\SomeHydrator');
My idea was that if I connect my ServiceManager like this:
$hydratorPluginManager->setServiceLocator($serviceManager)
It should work. But it doesn't and I am very confused why this is not working...
Am I missing something here?
You could just put the hydrators in the plugin manager directly. The hydrator plugin manager is configured in the same way it is for the service manager (invokables, factories, etc), only the config key is different, hydrators instead of service_manager
You just need to move your factories from service_manager to hydrators ...
'hydrators' => array(
'factories' => array(
'My\Hydrators\SomeHydrator' => 'My\Hydrators\SomeHydratorFactory'
)
),

Can the process of creating an module in zf2 be shortend?

I am looking at a few tutorials and to just create 1 module you have to modify a bunch of configuration files in order to make the controllers, models, and views work. I see this as impossible to try and remember it all or comprehend what it is I’m doing. Is there an alternative method that creates these for me? so that i don’t have to write it all out every time i create a controller, or a module etc. I honestly don’t see how this is faster. I come from a codeigniter background so making this switch has me banging my head against the wall multiple times trying to comprehend.
I've been using ZF2 for a few months now, and I've found that writing a class to generate that config for you is helpful.
There is no tool out there to do that for you. But if you follow the following approach, you should come right quite quickly:
class Configurator {
public static function route($name, $url, $controller, $action='index') {
return array(
'router' => array(
...
),
);
}
# Other static configuring methods
...
}
Then in your config you use the Configurator like this:
use Configurator;
return array_merge_recursive(
array(
'view_manager' => array(
...
),
),
# Other top-level config
...
Configurator::route('home', '/', 'Application\Controller\Index'),
Configurator::route('other', '/other', 'Application\Controller\Other')
);
Your config is deep-merged by array_merge_recursive, utimately producing the config you want with your own custom-built generators. You're at liberty to configure whole sets of config with one method, so you can create a resource configurator which sets up the controller invokables, the routes, and anything else required in one method.
array_merge_recursive FTW!
Enjoy! :)

Doctrine 2 + ZF2 : Single Sign-On

I'm currently making the new version of my company's website using ZF2 and Doctrine 2.
I have some modules, (Application, Account, Project Manager, Support, etc.) and only one (Account) provides the login/logout and every Auth functions. The problem, is that with Doctrine2, the Auth isn't very easy to use... The docs isn't very descriptive too.
If someone can help me, I would like to know how I use it with Zend\Authentication\AuthenticationService...
What I already know
In Module.php:
'factories' => array(
'Zend\Authentication\AuthenticationService' => function ($sm) {
$auth = new AuthenticationService();
$authAdapter = null; // What I do here??
$auth->setAdapter($authAdapter);
return $auth;
},
// ...
),
// ...
What I need to do to get the AuthService in every module, without "factory-sing" in each one...
Note: I'll be using the Identity View Helper...

Zend Framework 2.1, output from one controller action to another controller

I am using zend framework 2.1. I am trying to load the output of my indexAction from my login controller inside of my index controller. The end result I am trying accomplish is to just have my login form loaded on the index page as if it is part of that view.
I have searched for a few hours with no avail. I have attempted to use $this->view->action, which i've seen in earlier versions of zf2 but that has not worked either.
Any information would be helpful.
Taken from this blog, which explains in depth why $this->view->action() has been removed from ZF2, an example how to use the forward() (ZF2 documentation) controller plugin:
You can forward all necessary data to another controller inside your index controller action using the forward() controller plugin like this:
public function indexAction() {
$view = new ViewModel();
$login_param = $this->params('login_param');
$login = $this->forward()->dispatch('App\Controller\LoginController', array(
'action' => 'display',
'login_param' => $login_param
));
$view->addChild($login, 'login');
return $view;
}
In your view, all you need to do is:
<?php echo $this->login; ?>
Please note that the forward() plugin might return a Zend\Http\PhpEnvironment\Response instead. This happens if you use a redirect() in your login controller / action.
Also, if the Servicemanager claims to not find App\Controller\LoginController, have a look in your module.config.php. Look for a section called controllers.
Example:
[...]
'controllers' => array(
'invokables' => array(
'LoginCon' => 'App\Controller\LoginController',
'IndexCon' => 'App\Controller\IndexController',
'DataCon' => 'App\Controller\DataController',
)
),
[...]
Here, there is an alias for your login controller called LoginCon, you should use this name as controller name in the dispatch() method instead.

Categories