Advertisement
Guest User

zend framework 2

a guest
Feb 28th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. <?php
  2. /*
  3. \vedor\module\Application\Controller\LoginController.php
  4. */
  5.  namespace Application\Controller;
  6.  use Zend\Mvc\Controller\AbstractActionController;
  7.  use Zend\View\Model\ViewModel;
  8.  use Application\Model\SuperuserTable;
  9.  
  10.  
  11.  class LoginController extends AbstractActionController
  12.  {
  13.     protected $superuserTable;
  14.  
  15.      public function indexAction()
  16.      {
  17.         print_r($this->getServiceLocator()->get('Application\Model\SuperuserTable'));
  18.         return new ViewModel([
  19.             'superusers' => $this->superuserTable->fetchAll()
  20.         ]);
  21.      }
  22.  
  23.      public function getSuperuserTable()
  24.      {
  25.         if(!$this->superuserTable) {
  26.             $sm = $this->getServiceLocator();
  27.             $this->superuserTable = $sm->get('Application\Model\SuperuserTable');
  28.         }
  29.  
  30.         return $this->superuserTable;
  31.      }
  32.  }
  33. ?>
  34.  
  35.  
  36. <?php
  37. /*
  38. \vendor\module\Application\src\Application\Model\Superuser.php
  39. */
  40. namespace Application\Model;
  41.  
  42. class Superuser
  43. {
  44.     public $id;
  45.     public $name;
  46.     public $department_id;
  47.     public $image_profile;
  48.     public $email;
  49.     public $username;
  50.     public $password;
  51.  
  52.     public function exchangeArray(array $data)
  53.     {
  54.         $this->id= !empty($data['id']) ? $data['id'] : null;
  55.         $this->name= !empty($data['name']) ? $data['name'] : null;
  56.         $this->department_id= !empty($data['department_id']) ? $data['department_id'] : null;
  57.         $this->image_profile= !empty($data['image_profile']) ? $data['image_profile'] : null;
  58.         $this->email= !empty($data['email']) ? $data['email'] : null;
  59.         $this->username= !empty($data['username']) ? $data['username'] : null;
  60.         $this->password= !empty($data['password']) ? $data['password'] : null;
  61.     }
  62. }
  63. ?>
  64.  
  65. <?php
  66. /*
  67. \vendor\module\Application\src\Application\Model\SuperuserTable.php
  68. */
  69. namespace Application\Model;
  70.  
  71. use RuntimeException;
  72. use Zend\Db\TableGateway\TableGateway;
  73.  
  74. class SuperuserTable
  75. {
  76.     private $tableGateway;
  77.  
  78.     public function __construct(TableGatewayInterface $tableGateway)
  79.     {
  80.         $this->tableGateway = $tableGateway;
  81.     }
  82.  
  83.     public function fetchAll()
  84.     {
  85.         return $this->tableGateway->select();
  86.     }
  87.  
  88.     public function getSuperuser($id)
  89.     {
  90.         $id = (int)$id;
  91.         $rowset = $this->tableGateway->select(['id', $id]);
  92.         $row = $rowset->current();
  93.         if(! $row) {
  94.             throw new RuntimeException(sprintf(
  95.                 'Could not find row with identifier $id',
  96.                 $id
  97.             ));
  98.         }
  99.  
  100.         return $row;
  101.     }
  102.  
  103.     public function saveSuperuser(Superuser $superuser)
  104.     {
  105.         $data = [
  106.             'name'      => $superuser->name,
  107.             'department_id' => $superuser->department_id,
  108.             'image_profile' => $superuser->image_profile,
  109.             'email'     => $superuser->email,
  110.             'username'      => $superuser->username,
  111.             'password'      => $superuser->password,
  112.         ];
  113.  
  114.         $id = (int) $superuser->id;
  115.  
  116.         if($id == 0) {
  117.             $this->tableGateway->insert($data);
  118.             return;
  119.         }
  120.  
  121.         if(! $this->getSuperuser($id)) {
  122.             throw new RuntimeException(sprintf(
  123.                 'Cannot update album with identifier %d; does not exist',
  124.                 $id
  125.             ));
  126.         }
  127.  
  128.         $this->tableGateway->update($data, ['id'] => $id);
  129.     }
  130.  
  131.     public function deleteSuperuser($id)
  132.     {
  133.         $this->tableGateway->delete(['id', (int) $id]);
  134.     }
  135. }
  136. ?>
  137.  
  138. <?php
  139. /*
  140. *\vendor\module\Application\Module.php
  141. */
  142.  
  143. namespace Application;
  144.  
  145. use Zend\Mvc\ModuleRouteListener;
  146. use Zend\Mvc\MvcEvent;
  147. use Application\Model\Superuser;
  148. use Appication\Model\SuperuserTable;
  149. use Zend\Db\ResultSet\ResultSet;
  150. use Zend\Db\TableGateway\TableGateway;
  151.  
  152. class Module
  153. {
  154.     public function onBootstrap(MvcEvent $e)
  155.     {
  156.         $eventManager        = $e->getApplication()->getEventManager();
  157.         $moduleRouteListener = new ModuleRouteListener();
  158.         $moduleRouteListener->attach($eventManager);
  159.     }
  160.  
  161.     public function getConfig()
  162.     {
  163.         return include __DIR__ . '/config/module.config.php';
  164.     }
  165.  
  166.     public function getAutoloaderConfig()
  167.     {
  168.         return array(
  169.             'Zend\Loader\StandardAutoloader' => array(
  170.                 'namespaces' => array(
  171.                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
  172.                 ),
  173.             ),
  174.         );
  175.     }
  176.  
  177.     public function getServiceConfig()
  178.     {
  179.         return [
  180.             'factories' => [
  181.                 'Application\Model\SuperuserTable' => function($sm) {
  182.                         $tableGateway = $sm->get('SuperuserTableGateway');
  183.                         $table = new SuperuserTable($tableGateway);
  184.                         return $table;
  185.                 },
  186.                 'AlbumTableGateway' => function ($sm) {
  187.                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
  188.                     $resultSetPrototype = new ResultSet();
  189.                     $resultSetPrototype->setArrayObectPrototype(new Superuser());
  190.                     return new TableGateway('superuser', $dbAdapter, null, $resultSetPrototype);
  191.                 }
  192.             ],
  193.         ];
  194.     }
  195. }
  196. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement