Apply for Zend Framework Certification Training

Zend Framework 2




<?php
namespace Application;

use Zend\Db\TableGateway\TableGateway;
use Application\Model\Hotel;
use Application\Model\Employee;
use Zend\Db\ResultSet\ResultSet;
use Application\Model\HotelTable;
use Application\Model\EmployeeTable;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    public function getServiceConfig()
    { 
    	return array(
    			'factories'=>array(
    					'Application\Model\HotelTable'=>function($sm)
	    					{
	    						$tableGateway=$sm->get('HotelTableGateway');
	    						$table=new HotelTable($tableGateway);
	    						return $table;
	    					},
	    				'HotelTableGateway'=>function($sm)
		    				{
		    					$dbadapter=$sm->get('Zend\Db\Adapter\Adapter');
		    					$resultPrototype= new ResultSet();
		    					$resultPrototype->setArrayObjectPrototype(new Hotel());
		    					return new TableGateway('hotel', $dbadapter,null,$resultPrototype);
		    				},
                        'Application\Model\EmployeeTable'=>function($sm)
                            {
                                $tableGateway=$sm->get('EmployeeTableGateway');
                                $table=new EmployeeTable($tableGateway);
                                return $table;
                            },
                        'EmployeeTableGateway'=>function($sm)
                            {
                                $dbadapter=$sm->get('Zend\Db\Adapter\Adapter');
                                $resultPrototype= new ResultSet();
                                $resultPrototype->setArrayObjectPrototype(new Employee());
                                return new TableGateway('employee', $dbadapter,null,$resultPrototype);
                            },
    					)
    			);
    }
}

In Controller 

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
	protected $hotelTable;
	protected $employeeTable;
	public function getHotelTable()
	{
		if(!$this->hotelTable)
		{
			$sm=$this->getServiceLocator();
			$this->hotelTable= $sm->get('Application\Model\HotelTable');
		}
		return $this->hotelTable;
	}
	public function getEmployeeTable()
	{
		if(!$this->employeeTable)
		{
			$sm=$this->getServiceLocator();
			$this->employeeTable= $sm->get('Application\Model\EmployeeTable');
		}
		return $this->employeeTable;
	}
    public function index234Action()
    {
        return new ViewModel();
    }
    public function indexAction()
    {
    	return new ViewModel(array('listhotel'=>$this->getEmployeeTable()->fetchall()));
    }
}

in Views

<table class="table">
 <tr>
     <th>Name</th>
     <th>Address</th>
     <th>Action</th>
 </tr>
 <?php foreach ($listhotel as $data) : ?>
 <tr>
     <td><?php echo $this->escapeHtml($data->name);?></td>
     <td><?php echo $this->escapeHtml($data->address);?></td>
     <td>
         <a href="<?php echo $this->url('application',
             array('action'=>'edit', 'id' => $data->id));?>">Edit</a>
         <a href="<?php echo $this->url('application',
             array('action'=>'delete', 'id' => $data->id));?>">Delete</a>
     </td>
 </tr>
 <?php endforeach; ?>
 </table>                                    

< First How to create module in zend framework 2 >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top