M
M
magary42015-02-28 00:09:08
Zend Framework
magary4, 2015-02-28 00:09:08

Explain the benefits of multiple abstractions using zfcUser as an example?

I can't figure out how to benefit from all of this. studied on this issue nemeryanno different information. I myself use OOP at the level of data grouping and the definition of common behavioral methods that I can use when inherited.
Well, I decided to use this module, please show me with a practical example what benefit can I get from what the authors have defined, various interfaces, implemented hydrators, mappers?
Well, for example, you need ajax authentication. my code (simplified)

public function loginAction() {
  $user = $this->getServiceLocator()->get('some-service-for-make-sql-request')->search(['email'=>$_REQUEST['email'],'pwd'=>$_REQUEST['pwd']]); // service makes some crypt for 'pwd' before sql request
  $result = $user ? ['message'=>'ok','userdata'=>$user] : ['message'=>'fail','userdata'=>$user];
  return new JsonModel($result);
}

how to do it on zfcuser? ask google here
.....
$this->getRequest()->getPost()->set('identity', $data['identity']);
$this->getRequest()->getPost()->set('credential', $data['credential']);
$this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
$adapter = $this->zfcUserAuthentication()->getAuthAdapter();
$adapter->prepareForAuthentication($this->getRequest());
$auth = $this->zfcUserAuthentication()->getAuthService()->authenticate($adapter);
if (!$auth->isValid()) {
......

and this is just a code snippet, let's take the
bjyauthorize module. Yes, you can configure access through the config. I will keep silent that the same thing can be done by writing 10 lines of code in module.php by checking the name of the controller module and action. Well, in a real project, the user is trying to edit a record, I need to check his user_id in this record or someone else's. as?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas Lindemann, 2015-02-28
@LN

  1. ZF2 is "pure" PHP based on php-patteens. It does not have ready-made solutions for the implementation of this or that functionality, everything must be done manually. To understand its low-level abstraction layer, you need to know PHP itself at a very high level, its subtleties and nuances, built-in features, and understand php-patteens, because there are an unmeasured number of them. I really like to compare PHP-framework with music, and ZF2 is associated with Rammstein, that is, very hard (hardcore), why do I mean all this - to the fact that this framework has a very high entry threshold, and in difficult to learn even for advanced developers. This does not mean that it is bad, yes - it is complex, but the web application on it will delight you with its performance, which cannot be compared with any PHP framework. (provided that you do not use ready-made third-party solutions,
  2. As for abstraction layers, in most cases you will be dealing with ServiceManager - it's just a container of other class instances. That is, if you created a form, then you can place it in this container, and later, when you need it, just extract it from there. (by default, the same object is returned, but you can change this, return a new object specifically for some case). You can also define some business logic class as a service, and use it at the level of the entire application (you can also configure it as you want). You can also connect the EventManager component to all this - and do some actions when any event occurs in the web application.
  3. A hydrator is a process of filling an object with data, or vice versa - extracting this data from an object. Data is in most cases the usual properties of the class, and at the output you will get an object in the form of an array, that is, an object without methods, with only one property. This is how it works in ZF2: the user enters data into the form and sends it to the server, the system receives it, converts ordinary string data using a hydrator into a prototype object (what I wrote above, into an object in the form of an array). What is this prototype object? As it was written above, this is an ordinary class with some properties. Properties must be either protected or private, and it is also allowed to use methods to access these properties (get and set) in this class. After our prototype object is filled with data, we can manipulate it, for example, put it in the database (of course, we must filter the data and check for validity both when receiving it from the form and placing it in the database). The process of filling data in the database is also handled by the hydrator.
  4. If you've worked with an ORM, you've probably noticed a lot of similarities. By and large, what I described above is implemented in DoctrineORM itself. It turns out that ZF2 has implemented its "own bike", which has not yet been brought to perfection. Although if you compare performance, then ZF2 is out of competition. But if you need to implement a large system, then the ZF2 alternative is not yet suitable, it will not be able to show your full potential of the system. You can try to add this "bicycle", or create your own object-relational mapping system based on it, but for this you need to have very great knowledge in this area. By the way, to achieve performance, you need to use the mysqli extension, not PDO.
  5. Based on what I said above, you should understand that Symfony is probably quite suitable for your tasks, because you will have to spend a lot of resources to at least somehow do something worthwhile on ZF2. After all, if you add DoctrineORM and Twig template engine to ZF2, then the framework itself will become no better than SF2, I would say even worse, because performance will drop and it will be very difficult to maintain. Therefore, I personally decided for myself, if I need to do something really worthwhile, with great performance, and so that it is purely pure PHP, then I will choose the Zend Framework; and if I need a web application with moderate performance, and make it with minimal effort, then I will choose Symfony. In any case, choose what is best for your task, the choice is yours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question