Zend Framework the first freshness, CH1: sanderum MVC

unban Me here at the request of the Dean, and I decided to benefit society. Because I'm stupid and can't do anything so I thought I would write about ZF — awesome thing that all somehow truly hate. We need to do, however, is a NTV did not show Pelevin (sorry, not real).

The main trouble with all sorts of QS is that they are really quick and really start, but if you do everything as there will not be a particularly "scalable" application, which is not very clear what to do. Compared to the official quickstart'om the last version of Zend Framework, new is simply gorgeous, but not without flaws. I'll go on the other end: instead of explains how to do something meaningless and generic, I'll try (!) to tell you how to write the application at all, using various methods of the Zend Framework. And, it's time for Harakat to do.

Zend Framework — a magical and happy framework in PHP for real men ( ;-) ). To me, ZF is the only way to write legkodimova, masshtabnoe and quite soprovojdeno web application in a very short period of time (for 6 hours and easily done without much straining set authorization+check+posts+feed+tree comments+tea+coffee+let's dance), is different from the solutions, where in General almost nothing to do don't have (Wordpress, Drupal, ...) that the programmer is not imposed anything at all: ZF, in principle, a set of related classes to use only those that you want while keeping the style of the development, which you like best. You do not want to use Zend_Auth for login? And who you are forbidden to write anything? ;) He's beautiful and very logically built (all sorts of classes, parent, interfaces, all garbage) it is easy to rewrite, for example, simply having inherited some of his class from the standard endoscope and write what I want to write (but do not throw in the comments holivar "ZF against something." Please. Do not start).

Yeah! So, what we will get will be (hopefully) difficult, but it is logical and more or less beautiful: it will be easy to modify and easy to understand where we have that. I'm an idiot and an Amateur, and therefore can not claim that all my moves are really reasonable, correct and that I: for valuable advice, adding, disclaimers will say "thank you" and so on. Oh yeah, and I think that one article here is not limited. Moreover, the first may seem rather trivial, in this case, wait 8 second) in that particular I will talk about working with Zend'ovsky the implementation of the MVC, later going to stick with her, but I hope not to forget about the employees who don't like it.

Decided to repeat: "I am not a hacker, I'm just learning". Know better? Let's rewrite it/write the following article together, so that was even cooler ;-)

And Yes, I love you very much, but will be in the comments to point out grammatical or punctuation errors or typos in the code — Nasr in karma. Write to gabriotto. No one needs these temporary fixes, looming under the article, which can not remove.

Go!

The first step is download ZF and then run through QuickStart, at least until the "Create a Configuration and Registry". Then there is some SQLite stuff (I'll never understand the meaning of sulita), and more generally Zend_Form is used (I am opposed to this kind of entertainment, sorice — all the html we will write handles or asking web designers if you want to deal with it yourself, nothing complicated no). From that backbone that you have, and we'll take it from there.

So we have approximately the following (which is not — Domazlice if something extra — well, you figure out what to do with it):
the
+ application/
+ config/
* app.ini
+ controllers/
* IndexController.php
* ErrorController.php

+ scripts/
* layout.html
+ helpers/
+ models/
+ dbtable/
* SuperModel.php
+ views/
+ scripts/
+ index/
* index.phtml
+ error/
* error.phtml
+ helpers/
* routes.php [1]
* bootstrap.php
+ library/ // [2]
+ zend/
// TRU-La-La...
+ public/ (aka htdocs/ I, but it doesn't matter)
+ static/
// [3]
* index.php


[1] — file are all ranting. He included then bootstrap.php (after qs ranting needs to go in bootstrap, but usually I work out too much)
[2] — I would put this folder at the level above, that is to say, if you have it all in /home/va1en0k/coolsite.ru/, why not keep the libraries in /home/va1en0k/library/ instead of /home/va1en0k/coolsite.EN/library/: the rest of the prog will be easier to reach them
[3] — there will be all kinds of cool files CSS, JS, images — all canoe. They can be spread by styles/, images/, you may move to some other server, nobody is forcing you to keep them here: HTML all we do write

Quite simply, the conventional MVC. Algorithm write a program like this (it somewhat similar to the "TDD" which is so beloved, but, in principle, possible to arrange the tests view, controller and model before/after/during the development):
    the
  1. To start, we need something that occurs when a specific action of the user: us designer asked or there is something else;
  2. the
  3. Action — always a challenge Urla any. Come up with url (it can be any, but we are all very happy URLs of the form /"controller_name"/"action_name". But, say, to get the post better than any of this: /post/12321, isn't it?), decide how it will be handled by the controller (it does not fit? Create a new /application/controllers/) added to this controller action;
  4. the
  5. Create /views/scripts/nazvanie/naslanjati.phtml, draw there we need the html;
    the

      where you need to withdraw something that is not displayed on other pages, and write: <?=$this->shachlo?>, record or remember what we need;

      where you need to withdraw something that appears many places, for example, a user write something like <?=$this->printUser($this->user)?>;


  6. the
  7. Now go and create all the helpers we need (helpers are functions that are called like this: <?=$this->printUser($this->user)?>). Write them in /views/helpers/. It is better to take them separately in order to be able at any moment to the names of users to add any icon or apply any other style. Or replace them all on a word MACLO to all users just ohreneli surprise! %)
  8. the
  9. Bouncing around from happiness, I write action. The full code for the action (why not actually) to be divided into 3 parts:
    the
      the
    • Deal with arguments: receive data from forms or there from the url (if the url was /user/12345);
    • the
    • Do what you want from the action: launch a rocket into space or send a message to the taxi driver on the pager. If we need data from the database, or if we need data from some Last.fm or Twitter or even any data — any at all — ask them about the model. If model [yet] can't give, please like you can: $this->m->twitter->getUser("va1ka"); or is there a $this->m->getUsersTable()->getUser("va1en0k"); — as you want, and request;
    • the
    • Give the view everything it asked for in step 3: $this->view->shachlo = "popyachtsa"; etc.;
  10. the
  11. Append to model everything we have asked it in the previous step.


This algorithm is near-universal (if we need something that occurs when any action of the user, it is necessary to act slightly differently), and I recommend it as clearly as possible. Why first, and then the rest? Well, for two reasons, first, so suggest 37signals, and secondly, it is the most reasonable way to understand what we need and in what form to obtain. Important to make sure that all requests to the data was only inkapsulirovannoj through the model: then, the happiness we can in which case not to give up looking database to another or to add a cute cache, accelerating our programme, as the reset speed of the module. It is important to make sure that all the HTML, well, that is, entirely whole, was going through the folders, views or layouts, helpers or just because.
And now specifically on the three pillars of MVC.

Views

As I have eloquently hinted, the Types of needs to be whole-the whole HTML. Any mistakes, anything that happens in the controller — especially in the model should be devoid of tags and must be a bare string. Any constant o (I mean, for example, tagline or "Error! A user name must be 25 characters! Kill yourself!") also worth to keep in view.

How to do it? Hurry to help the helpers! Create a helper (then there is the amazing code that is not the fact that you should use as is, but, in principle, there is nothing quite terrible. Just think for yourself how you want to organize the events):
the
Zend_View_Helper_Errors class extends Zend_View_Helper_Abstract {
public function errors($err) {
if (!sizeof($err))  return  ";

$html = '<div class="errors">';

foreach ($err as $er)
$html .= '<p>'. AppErrors::getErrorString($er) .'</p>';

$html .= '</div>';

return $html;
}
}

And a class with a static method:
the
class AppErrors {
static function getErrorString($er) {
// the returned string. Take it from someone: array there, or DB or somewhere else, translate, if necessary, using Zend_Translate and return
}
}


Itself the $err argument to helper is an array of "errors". The error can be encoded simply by the number some, and can be, and array, say, array( 'err_id' => 666, 'err_arg' => 'satan' ), if there are errors the arguments of %) Well or even object who have much fantasy perverted.

Controller

The controller consists of actions. Action — the backbone (as in structural-functional sociology on the basis of the integration approach) applications. So all looks perfect request from the designer of the program to the programmer: "Come on, if you press that knopochku will be that." Here, the "something" this is the action (who am I explain it?).

Before you produce controllers in batches, I suggest you do a cunning move to create our own front and action controllers and inherit from them. Then, if anything, we can, for example, to prohibit visits to any pages without login to the website (in a leper colony, for example), to exhibit different desired action of the controller variables, and so forth:
the
class SchachloFrontController extends Zend_Front_Controller {
// 
then do not forget to overload the getInstance () us doctor proposal
} // don't forget to change bootstrap.php!
class SchachloActionController extends Zend_Action_Controller {
// in my opinion, it not necessarily overload %)
} // inherit all action controllers from it (those that are in /application/controllers/


Well, and then everything more or less clear. Suggest possible to divide the code into small functions, shoving them into the model or leaving the share of helpers (like view helpers and action helpers). And don't forget: all data is to the model, even the middle name of your grandmother %) Although it is not, it is better in config if it's just for your grandma.

Model

With the model, in fact, I have the most doubt of inexperience. Well, I personally usually do like this:
For each data type, create signs in the database. There to post, commentary, the works. If the data type of a variable is an array (for example, the tags for the post), it, obviously, a separate plate. Then for each plate is created in /application/models/DbTable/ your file. Well, the main file — SuperModel.php in which our lovely Angelina Jolie returns, its methods, all the signs (which she stored in static variables). Similarly, shoved next to the desired classes for working with the file system or external data sources (recently here with last.fm worked with Zend_Rest_Client generally without tension), and supermodel shove the methods to obtain them.

So, if I suddenly need to know something, I look at how many sources of data it concerns. If same sign: add a method in its own class. If one plate and a few more for her (like, we get a post from the table to posts and tags to it from the tags, etc.) is also something in her. But if we need 2 more-less equal table — put a method in the model, but work with each of the participating tables can be arranged in it.
Che is there to say? Ah, Yes. Do not forget about cache. You can add in each method of surveillance of a cache, but you can take and make 2 models: one will be the same as described above, and the other through cache trigger first, along with unplugging it when it is not needed. I mean the cache will still write me back if not banned. :D

UV. Like 13 kilobytes in size, but not very much and told. A lover of what is already here! Looking forward to your feedback.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Briefly on how to make your Qt geoservice plugin

Database replication PostgreSQL-based SymmetricDS

Yandex.Widget + adjustIFrameHeight + MooTools