Zend Framework 2 - php

I'm a big enthusiast about new Zend Framework. I had done one project in the previous version so far and I decided to learn new one which contains a really hard parts to understand for me.
I've watched webinars and youtube videos, also I read the documentation and I'm trying to understand this framework simply by read the code but there are few things I just can't figure out.
There is a lot information about EventManager in webinars but since I'm lack at English I'm just not able to understand 100% of the speech.
I know that event manager manage events which is quite obvious but I don't really know how to use it: in one of the webinars they provide some simple examples but they don't explain where to put this code, is it module.php file? Or some other place if you can show me some practical example with explanation so i can see whats the point.
Next thing is that mysterious $e passed as an argument to most of the functions in Module.php which after is used like this: $e->getApplication or something I just wonder what this $e stands for? It is instance of what? And next thing is how it is passed "automatically" to these functions?
Another problem: in configuration files which are specified for each module called module.config.php there is a lot of things. I got the point of routes I've understood them but I can't figure out what are the invokables and factories. It is also explained in one of the webinars as follow: invokables are paths to classes, factories are callable functions or classes (cant remember). The point is OK, that make sense in some theoretical talk but please provide me some examples with in depth explanation, where those factories and invokables come from and other stuff that I need to know.
The other thing is that there is a lot of configuration possibilities in those configuration files. Where can I find some informations about what "keys"=>"values" are possible in those files?
That's all for now but there are still unanswered questions i would like to ask. If someone is able to help me, I would really appreciate to correspond via mail with this person.
Ok one problem is solved. The one about mysterious &e so if somene is intersted in here is a solution:
This $e variable instance depends on what function we use in module.php file:
case 1: init() -> &e is an instance of module manager
case 2: onBootstrap() -> &e is an instance of MvcEvent
(if there are other cases i havent found please let us know)
The thing is that those methods are called when event has been triggered so mysterious &e is passed to those functions by listenerers which are listening if these functions appears in our code (this is my simple logic of that so please dont hate me)
There are still other questions to answer.. as soon as ill figure out some senseful answer i will let you know

Within stock ZF2 code, $e is always an instance of Zend\EventManager\EventManagerInterface. Usually it is either an Zend\EventManager\Event or an Zend\Mvc\MvcEvent.
A class' EventManager triggers an event which results in listener callbacks being called. Each listener callback is passed an instance of an Event which then has some useful methods, notably getTarget() and getParams(). Other instances of EventManagerInterface usually have more specialised methods. MvcEvent in particular has methods related to the Mvc component, such as getApplication(), getRouter(), getRequest() and getResponse().

You can find more information about tghe EventManager, application configuration and MvcEvent in the "Using Zend Framework 2" book: http://leanpub.com/using-zend-framework-2

Related

What does it mean to hydrate and dehydrate a Laravel Livewire component?

Could someone point me to a definition of "hydrate" and "dehydrate" as it applies to Livewire components? The only hit in the documentation search refers to
Livewire will take care of hydrating and dehydrating the model between requests with the current, non-persisted data.
In the Lifecycle hooks section, the hydrate and dehydrate class hooks are self referential.
hydrate: Runs on every request after the component is hydrated...
Responses to similar questions have indicated that hydration is filling a object with data after it's been instantiated -- that kinda makes sense, but what would "dehydrating" an object would be?
I don't know if there is a good definition of the lifecycle in the documentation other than the hooks.
I'll explain my understanding of it.
So there are two levels of hydration essentially in Livewire, one is hydrating a component, and the other is hydrating the properties of a component.
Livewire needs to send data back and forth between php and javascript for it to work the way it does. As part of the messages that get sent there is information about the component, and information about the properties. But for the information to be accessible and usable by javascript it needs to be in the right format.
The process of hydration is taking the message and converting its values back into php, creating the Livewire component and creating any of the properties that component has.
Dehydration is the reverse, it goes through all the properties, and serialises them, then it serialises the component and bundles that all into a message that gets sent to the front end.
Caleb has addressed this very question in a blog entry. The post linked to below is also linked to from the Livewire Docs, though I admit that it is only linked to from a lowly sub-section of the documentation.
I really do think that information like this should be put front-and-centre to lay the groundwork for other people's understanding the framework. Documentation that requires additional foot-notes to gain a full understanding, is by definition, not Documentation, right!?! Anyway, point is, I don't blame anyone for missing it!
Blogpost found here >>
The official explanation isn't actually much different from the answer already accepted (#JoshHanley). Nevertheless, I do find the blog to be clearer and better explained, and might help others in future.
I know that (according to SO guidelines) I'm supposed to re-word the answer as well as link to a resource, but honestly there's no short way of explaining a framework like this. You just gotta jump into the deep end I'm afraid.

How to create router for a component in joomla?

How to create router file for a component of joomla. I am using Sef
url in particular
I was having the same issue and after trawling through the internet and only ever coming across answers like the one seen here ("see the documentation") which is really unhelpful in my opinion and of all the documentation that page is the most unhelpful.
Anyway I firstly ended up giving up on the "component router", I could get it to easily work to build routes but found that it wouldn't parse anything and without having both working, it was pointless continuing.
In the end I decided going down the plugin path was the answer and found this really good plugin here from Daniel Calviño Sánchez.
I then ended up finally coming across some joomla documentation here that IS perfect and will get you exactly where you need to go.
I personally think that the joomla router needs alot of work and saw there are lots of ideas from people wanting it upgraded. I found using a plugin in the end was the easiest path and was the better solution all round.
I would be happy to discuss with anyone as to why my component router wouldn't call its parse method as if that actually worked, it would've been my first choice.
Hope this helps.
Unfortunately documentation didn't provide a way to register my router so I found an alternative solution.
The router.php is actually a Joomla\CMS\Router\SiteRouter
You can use the $this variable, even if you think you're not within a class.
This can be checked by performing var_dump($this)
There are two methods that can be used.
$this->attachBuildRule(function(){
// build rule code
});
$this->attachParseRule(function(){
// parse rule code
});
If you are using an object implementing RouterInterface the callbacks are the following
$this->attachBuildRule([$myRouter, 'build']);
$this->attachParseRule([$myRouter, 'parse']);
The methods expect a callable and a STAGE, see Router::PROCESS_* constants in namespace Joomla\CMS\Router
If you are using PHPStorm you can write the following code for assistance:
/**
* #var $this Joomla\CMS\Router\SiteRouter
*/
Check out this documentation .
It describes very precisely how to create a Route and how the Routing works in Joomla.
Also check out com_content/router.php as an example

How do I avoid HMVC design pattern in Laravel?

So I have been reading through From Apprentice to Artisan by Taylor Otwell, Laravel Author
And I came across this 'mantra' : HMVC usually indicates poor design.
Which is kind of true...
Also Taylor has suggested
Feel the need to call controllers from other controllers? This is often indicative of poor
application design and too much business logic in your controllers. Extract the logic into a
third class that can be injected into any controller.
And I don't seem to find such way yet..
How can I avoid HMVC and extract the logici nto a third class that can be injected to any controller?
I came up with a neat way to do it, and it seems to have helped me speed up my workflow...
I think this alternative I made can replace HMVC, as well as the conventional way of using controllers... as now controllers are but somehwere where our -what I called- 'motors' are injected.
Check out my article at coderwall where I went through the whole thing.
Read through it, and hopefully it will provide a better way of doing things, starting from the models to and finishing at controllers.
However if you wish to proceed your own way, make sure what was required to be shared between two controllters hiarchecly gets shared in a more neater way, as taylor suggested, shared through injection.
For instance, you are in AdminsController and you feel the need to call an action from UsersController, just make that action and its siblings into a third class, and in your AdminsController
//AdminsController
use ThirdClass;
public function __construct(ThirdClass $mything)
{
$this->myThirdClass = $mything;
}
public function mySharedAction()
{
$this->myThirdClass->mySharedActionFromUsersController();
}
And like so.
Update
If you have gone through my article at coderwall, the one I have mentioned above, I have made a little package that generates all of the mentioned components in there.
Check it out at github

What parts of a PHP website are handled by a Dependency Injection Container?

After reading a trillion vague tutorials on Dependency Injection Containers, I feel as though I'm still not fully getting the point.
When it comes to things that are application specific (like models, controllers, etc), how should I manage the auto-injection of dependencies?
I happen to have a LARGE website, with maybe 30-40 different types of models...do I really create a single application dependency manager to handle each of these different types?
I have had people tell me that DIC's are not meant for domain stuff, but more for framework stuff, and I have also heard the opposite.
Which is "right"?
BONUS QUESTION:
If DIC's aren't applicable for domain layer objects (like models), how do you pass dependencies to the domain layer objects?
Maybe not exactly what you are looking for but here is an example usage of Dependency Injection Container (DIC)
Let's say I have a Database class and a Cache class. I need to be able to access my database and my Cache class inside of my other classes (models, controllers, etc).
This is a situation where a DIC would come in handy, I can simply store my Database and Cache class inside the DIC class and pass that DIC class into any other class that needs access to the objects that it holds
It's not very clear what the question is, but I'll try.
When it comes to things that are application specific (like models,
controllers, etc), how should I manage the auto-injection of
dependencies?
Do you use a DIC? Did you write one, or do you use a library?
Maybe a library would help, I am the author of PHP-DI so I'm biased, but the library helps me auto-inject dependencies.
For example I can write:
class UserController
{
private $formFactory;
public function __construct(FormFactory $formFactory) {
$this->formFactory = $formFactory;
}
public function createForm($type, $data, $options) {
// $this->formFactory->...
}
}
In that situation, the DIC can detect what dependencies your controller want, and autoinject them. That makes life easier.
I happen to have a LARGE website, with maybe 30-40 different types of
models...do I really create a single application dependency manager to
handle each of these different types?
Why would you need that? I don't understand the need, maybe you need to explain a bit more what's the problem.
I have had people tell me that DIC's are not meant for domain stuff,
but more for framework stuff, and I have also heard the opposite.
Which is "right"?
IMO there's no difference between "domain stuff" and "framework stuff". The only thing that is hard though, is having a DIC injecting stuff in an entity, because that's not a "singleton" (not in the Singleton Pattern way): these objects are not "managed" by the DIC, you can create instances manually everywhere in your code. Example:
$user = new User();
If the User class needs a service, then everywhere in your code you have to do:
$user = new User($service);
On the contrary, you never need to create a service manually (you never need to call "new"), so you can let the container create the instance and inject everything.
I'm not very happy with my answer as it's a bit vague, but I have trouble identifying really what's your problem. Maybe you should give code examples, or at least explain a bit more.
PS: I have struggled to understand DI and what is a DIC too, don't settle for a "I think I get it a bit more now, though not totally", I did the same and it took me months to finally understand.
If it helps, read the introduction text at http://mnapoli.github.io/PHP-DI/ and maybe also: http://mnapoli.fr/controllers-as-services/ (not directly related, but it can help)

Cannot find Function Definition of an Event Observer class in Magento

For anybody who has seen / used Magento, can you please tell me where can I find the following 3 function's definitions of the Catalog Product's save action's Event Observer class:-
setBundleOptionsData()
setBundleSelectionsData()
setCanSaveBundleSelections()
Please pardon me, for asking such a silly question, but I am really helpless.
Any help is greatly appreciated.
The worst part is that these above 3 methods are being used for the product object in a nice way, & they are working too. But where are their definitions?
EDIT:-
Okay, I can understand that these are used by the "Varien_Object", and these are simple setter functions using the concept of Magic Methods. But can somebody please tell what is the coding flow, when the program counter arrives at such a function, in this case in the Event Observer class?
If you have run a search on all the files and can't find the definition then these are most likely using PHP's magic methods. Is this class inheriting (directly or indirectly) from Varien_Object? If so, then these are simple setter functions storing data in an array within the object.
To get these values back all you need to do is change the 'set' to 'get':
$this->setBundleOptionsData('whatever');
echo $this->getBundleOptionsData(); //Returns 'whatever'
If you're interested in how this works, look inside class Varien_Object. I've also described the mechanism here.

Categories