πŸ“œ ⬆️ ⬇️

We write the Framework

PHP Frameworks
For beginners "cyclists" il just curious ...



This article is not a call to action, but only a small sketch on the topic "How I would do it." At the moment, my department actively uses the Zend Framework, and I’m best acquainted with it, so don’t be alarmed, it’s not advertising, because most frameworks combine advantages and disadvantages in equal measure, but we need only advantages ...
')

rules


I would start by regulating the rules:

Without developing these rules, you risk turning the framework into a garbage bin. Also, I strongly recommend writing unit tests - they will help save a lot of time.

Architecture


I hope most readers are already familiar with the MVC pattern ( Model-View-Controller ) - so let's base our framework on it, use something else, I'm afraid it will scare users (here I mean programmers :)).

Model


In a typical project, the model is tied to a single table in the database, but there are enough exceptions, so this statement should not be taken as an axiom. Our model should easily work with various data storages, be it a database, files, or memory.

Let's imagine what a model might look like:

// User model uses database as storage
class Model_User extends Framework_Model_Database
{
$ _table = "users" ;
$ _pkey = "id" ;

function getByLogin ( $ login ) { /*...*/ }
function getByEmail ( $ email ) { /*...*/ }
}

// model MainConfig uses ini file as storage
class Model_MainConfig extends Framework_Model_Ini
{
protected $ _file = "application.ini" ;

function setOption ( $ key ) { /*...*/ }
function getOption ( $ key ) { /*...*/ }
}

// the Registry model uses memory as storage - some alternative to global variables
class Model_Registry extends Framework_Model_Memory
{
function setOption ( $ key ) { /*...*/ }
function getOption ( $ key ) { /*...*/ }
}

// Session model uses session files as storage
class Model_Session extends Framework_Model_Session
{
protected $ _namespace = "global" ;

function setOption ( $ key ) { /*...*/ }
function getOption ( $ key ) { /*...*/ }
}


In fact, with such examples I strongly distort the idea of ​​MVC - in fact, often by model they mean some business model, but not a session or a configuration file.


View


What are the current requirements for the template engine? Personally for me, the native PHP syntax, support for various kinds of helpers and filters. The β€œTwo Step View pattern” pattern should also be implemented, in ZF there are two components for this - Zend_View and Zend_Layout .

I will give an example of such a presentation:
<? php if ( $ this -> books ) : ?>
<! - A table of several books. ->
<table>
<tr>
<th> Author </ th>
<th> Title </ th>
</ tr>
<? php foreach ( $ this -> books as $ key => $ val ) : ?>
<tr>
<td> <? php echo $ this -> escape ( $ val [ 'author' ] ) ?> </ td>
<td> <? php echo $ this -> escape ( $ val [ 'title' ] ) ?> </ td>
</ tr>
<? php endforeach ; ?>
</ table>
<? php else : ?>
<p> There are no books to display. </ p>
<? php endif ; ?>

An example of using layouts (taken from the Zend_Layout documentation):

Layout Example

Oh yes, in the Zend Framework, the successful implementation of the presentation, I like it, of course, not without minor complaints, but in general it is five.


Controller


β€” , β€” .

: example.com/?controller=users&action=profile&id=16

, β€” id=16. users profile, id:
//      -    
class Controller_Users extends Framework_Controller_Action
{
    public function actionProfile()
    {
        //    
        $id = $this->request->get('id');
 
        //  
        $user = new Model_User();
        $user -> getById($id);
 
        //    
        $this->view->user = $user;
    }
}


, , .. JSON , ( MVC, ).

Request'a β€” . β€” .


Routers


β€” . :

example.com/?controller=users&action=profile&id=16
example.com/users/profile/id/16 // URL'a ZF
example.com/users/profile/16 // CodeIgniter
example.com/profile/16 // ,


/ ZF β€” β€” URL', β€” .

β€” URL , :
example.com/users/list/page/2/limit/20/filter/active

example.com/users/list/2/20/active


Request, , URL View β€” Request β€” , , Request View


Request & Response


Request β€” :

Response β€” , :

Response ZF β€”


Modules


, .. - (, , ..) . MVC , - .

Core


β€” , , :

  1. Router', Request
  2. Request , //
  3. ( )
  4. Response

, .


«», :


Zend Framewrok' β€” , , .. Zend'a ( : ZYM)



( ):



, CMS β€” , .

«» β€” ...



, , ( document_root public):
project
|-- application
|    |-- configs
|    |-- layouts
|    |-- controllers
|    |-- models
|    |-- views
|    `-- modules
|         `-- <module_name>
|              |-- layouts
|              |-- controllers
|              |-- models
|              `-- views
|-- data
|    |-- cache
|    |-- logs
|    `-- sessions
|-- library
|    `-- Framework
|-- public
|    |-- styles
|    |-- scripts
|    |-- images
|    |-- uploads
|    |-- .htaccess
|    `-- index.php
`-- tests


, β€” …


To Be Or Not To Be β€” , β€” - , . , , β€” .

RSS http://anton.shevchuk.name/feed/, http://twitter.com/AntonShevchuk

Source: https://habr.com/ru/post/68093/


All Articles