Converting a Laravel project to the Zend Framework 1 project

,

Laravel Eloquent ORM code to Zend_Db_Table_Row:

Laravel Eloquent Insert, Update, Delete : https://laravel.com/docs/4.2/eloquent#insert-update-delete

Zend_Db_Table_Row is a class that contains an individual row of a Zend_Db_Table object. When you run a query against a Table class, the result is returned in a set of Zend_Db_Table_Row objects. You can also use this object to create new rows and add them to the database table.

http://framework.zend.com/manual/1.12/en/zend.db.table.row.html

Zend Framework 2 – Database access pattern

,

TableGateway-ről, de akár, a Zend Framework 2 -ről : Yes, for small projects it can be, or seem like, overkill. But as project complexity grows, as it invariably does, you won’t miss the time not spent maintaining your application.

TableGateway ,RowGateway https://leanpub.com/using-zend-framework-2/read

Feature
ZF2
Symfony 2
Cake PHP
CodeIgniter
Yii
Current version
2.2.1
2.3.1
2.3.6
2.1.3
1.1.13
Distribution
3 Mb
4.4 Mb
2.0 Mb
2.2 Mb
3.9 Mb
archive size
Installation
Composer
Composer
Archive
Archive
Archive
Compatibility
Bad (requires
Bad (requires
Good
Good
Good
with
SSH and
SSH and
shared hostings
vhosts)
vhosts)
Monolithic or
Components
Components
Components
Components
Monolithic
component-
based?
Prefer
Configuration
Configuration
Conventions
Conventions
Conventions
conventions
or configs?
Database access
Data Mapper
Data Mapper
Active Record
Traditional
Active Record,
pattern
(Doctrine/ORM),
(Doctrine/ORM)
or
PDO
Table Gateway,
Active Record
Row Gateway
Database
Yes (Doctrine-
Yes (Doctrine-
Yes
Yes
Yes
migrations
provided)
provided)
CSS Framework
Twitter
Twitter
Any
Any
Blueprint CSS
Bootstrap
Bootstrap
View Template
Any you want, or
Twig
Smarty/Twig
Any you want,
None or Prado
Language
none at all
(recommended)
or none
Unit testing
Yes (PHPUnit)
Yes (PHPUnit)
Yes (PHPUnit)
Yes (PHPUnit)
Yes (PHPUnit-
support
based)
Functional
Yes (PHPUnit)
Yes (PHPUnit)
Yes
No
Yes (Selenium)
testing
Database
Yes (Doctrine-
Yes (Doctrine
Yes
No
Yes
fixtures
provided)
bundle)

ZendDbTableGateway¶

Are TableGateways Worth it in Zend Framework 2?

Az üzleti logika helye: The TableGateway class contains table-aware logic, such as queries to fetch all and fetch by specific criteria. The model class is table agnostic. It only knows about the properties that it needs data for.

Then, we have the module configuration, which binds the two together. Firstly, we have a TableGateway configuration, so we know which table we’re binding to the TableGateway. Then we have the table configuration. I’ve taken this configuration further by using a HydratingResultset object. What this does, is to also implement the RowGateway pattern.

Zend Framework2: Building a CRUD app by using Table Gateway1

Zend Framework 2 – routing & problem

,

Routing : http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html

http://zf-tutorial/album/add – 200 – works fine
http://zf-tutorial/album/add/ – 404 – The requested URL could not be matched by routing.

To handle that, just add the empty “/” in the end of route segment :

‘route’ => ‘/[:controller[/[:action[/]]]]’,

Or for “:slug” segment :

‘route’ => ‘/[:controller[/[:action[/[:slug[/]]]]]]’,

http://samsonasik.wordpress.com/2012/07/24/zend-framework-2-trailing-slash/

Zend framework 2 info

,

Forms and actions – Albums example

 

Matthew Setter Running the ZF2Skeleton with PHP’s Built-in Webserver

Zend Framework 2 CheetSheet
Zend FRamework 2 module directory structure

A route -olás, útválasztás miatt szükséges az src-n belül ismételni a modul nevét. (?)

module/
    Account/
        config/
        src/
            Account/
                Controller/
                    CaseController.php (AccountControllerCaseController)
                Entity/
                    CaseEntity.php     (AccountEntityCaseEntity)
                Form/
                    CaseForm.php       (AccountFormCaseForm)
                Mapper/
                    CaseMapper.php     (AccountMapperCaseMapper)
                Service/
                    CaseService.php    (AccountServiceCaseService)
        view/
        Module.php

 

Zend Framework 2 for Newbies

Fájl fetöltés. (nem csak a Standard megoldás) A manual megoldást mutat a feltöltés + adat űrlap ellenőrzésre. Ha az adat, pl. email cím nem érvényes, a feltöltött képet nem dobja el. http://framework.zend.com/manual/2.1/en/modules/zend.form.file-upload.html “When using other standard form inputs (i.e. text, checkbox, select, etc.) along with file inputs in a Form, you can encounter a situation where some inputs may become invalid and the user must re-select the file and re-upload. PHP will delete uploaded files from the temporary directory at the end of the request if it has not been moved away or renamed. Re-uploading a valid file each time another form input is invalid is inefficient and annoying to users.”

Params http://stackoverflow.com/questions/12077126/how-to-access-route-post-get-etc-parameters-in-zend-framework-2

$this->params()->fromPost('paramname');   // From POST
$this->params()->fromQuery('paramname');  // From GET
$this->params()->fromRoute('paramname');  // From RouteMatch
$this->params()->fromHeader('paramname'); // From header
$this->params()->fromFiles('paramname');  // From file being uploaded

View Helper – URL

Database and models¶

 

ZendAuthentication
Create Simple Login Authentication
Database Table Authentication¶

Session Container

Using Sessions in Zend Framework 2

ZendPermissionsAcl

Zend Framework 2: Authentication + Acl using EventManager

Zend Framework 2 ACL setup in 5 minutes – tutorial

AuthenticationService and Session Db

Zend Framework ACL Plugin

http://phptechsolutions.wordpress.com/2012/07/06/building-a-simple-zend-framework-acl/

Zend_Acl consists of resources, privileges and roles. Resources can be anything ranging from controllers to files. Privileges are different levels of access on the resource. Roles determine who can access a resource, and with what privileges. Roles can be users, user groups or anything you wish to associate. In Zend_Acl, Role can be inherited form one or more roles.

To create resources and roles, you will need to first create Zend_Acl instance as

1
$acl = new Zend_Acl();

And then add role and resources to it as follows

1
2
3
$acl->add(new Zend_Acl_Resource(‘view’));
$acl->add(new Zend_Acl_Resource(‘edit’));
$acl->add(new Zend_Acl_Resource(‘delete’));

Once we create roles and resources we can assign different privileges to different roles on different resources as

1
2
3
$acl->allow(‘guest’,null,’view’);
$acl->allow(‘editor’,array(‘view’,’edit’));
$acl->allow(‘admin’);

Similarly we can use deny() method of Zend_Acl for access denials as

1
$acl->deny(‘guest’,null,array(’edit’,’delete’));

Later in our code we can check privileges as

1
$acl->isAllowed(‘guest’,null,’view’);

isAllowed() method return boolean value either true or false based on the privileges.
To see how can we use Zend_Acl component in our applications lets take a simple example.

Let we have different controllers, e.g news, latestnews, announcements with each having the view, edit and delete actions

Now in Library/My/Controller/Plugin/, create Acl.php and place the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
 class My_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
 {
     private $_acl;
     public function preDispatch(Zend_Controller_Request_Abstract $request)
     {
         $acl = $this->_getAcl();
         $role = $this->_getRole();
         $resource = $request->getControllerName();
         $privilege = $request->getActionName();
         $allowed = $acl->isAllowed($role, $resource, $privilege);
         if (!$allowed) {
             $controller = 'error';
             $action = 'index';
             $redirector = new Zend_Controller_Action_Helper_Redirector();
             $redirector->gotoSimpleAndExit($action, $controller);
         }
     }
     protected function _getAcl()
     {
         if (null === $this->_acl) {
             $acl = new Zend_Acl();
             // Roles
             $acl->addRole('guest');
             $acl->addRole('user', 'guest');
             $acl->addRole('admin', 'user');
             // Resources
             $acl->add(new Zend_Acl_Resource(‘view’));
             $acl->add(new Zend_Acl_Resource(‘edit’));
             $acl->add(new Zend_Acl_Resource(‘delete’));
             // Rules
             $this->acl->allow(‘guest’,null,’view’);
             $this->acl->allow(‘editor’,array(‘view’,’edit’));
             $this->acl->allow(‘admin’);
             $this->_acl = $acl;
         }
         return $this->_acl;
     }
     protected function _getRole()
     {
         $auth = Zend_Auth::getInstance();
         if ($auth->hasIdentity()) {
         $identity = $auth->getIdentity();
         $role = empty($identity->role) ? 'user': $identity->role;
         } else {
             $role = 'guest';
         }
         return $role;
     }
 }

Explanation:
In the code above we are creating plugin by extending it form Zend_Controller_Plugin_Abstract and override preDispatch() method.

If this is first attempt to access our application we give user a role “guest”. We can set this type at our authentication and give user a specific type when he login.

Next we get Action name by using $request->getActionName() and assign it to $privilageName.
The next line are very crucial. We check the privileges

1
$allowed = $acl->isAllowed($role, $resource, $privilege);     

If the above condition is true. It means that the user hasn’t had the privileges to access the requested Action.

So we redirect user to ErrorController’s Index action.

If the condition is false then he access the particular controller action.

We have now nearly done. However you will need to register the plugin.

1
2
3
// application/configs/application.ini
autoloaderNamespaces[] = "My_"
resources.frontController.plugins.Acl = "My_Controller_Plugin_Acl"

That it your simple role management application.
Now if you first request

http://yourhost/news/view

it will give you access to the specified view Action of the news or any other controller.
However if you request

http://yourhost/news/edit/

you will be redirected to the Error Controller’s index action. I haven’t mention Error Controller, so you better create your own.