zend framework,How to start with zf2,begining with zendframework 2 Reviewed by Momizat on . First install zend framework after then set some configaration zend model configuration 1. Database connection (config/autoload/gobal.php) copy and paste this c First install zend framework after then set some configaration zend model configuration 1. Database connection (config/autoload/gobal.php) copy and paste this c Rating: 0

zend framework,How to start with zf2,begining with zendframework 2

First install zend framework after then set some configaration

zend model configuration

  • 1. Database connection (config/autoload/gobal.php)
  • copy and paste this code (only change database name,hostname)

    return array(
         'db' => array(
             'driver'         => 'Pdo',
             'dsn'            => 'mysql:dbname=zend;host=localhost',
             'driver_options' => array(
                 PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
             ),
         ),
         'service_manager' => array(
             'factories' => array(
                 'Zend\Db\Adapter\Adapter'
                         => 'Zend\Db\Adapter\AdapterServiceFactory',
             ),
         ),
     );
    
  • 2. database user connection (config/autoload/local.php)
  • // copy and paste this code (only change username,password)
     return array(
         'db' => array(
             'username' => 'root',
             'password' => 'root',
         ),
     );
    
    
  • 3. Here Booklist is a module (BookList/module.php
  •  <?php
    
    namespace BookList;
    
    use BookList\Model\Bookmodel;
    
    class Module {
    
        public function getAutoloaderConfig() {
            return array(
                'Zend\Loader\ClassMapAutoloader' => array(// THIS IS
                    __DIR__ . '/autoload_classmap.php'      // THE PROBABLE
                ), // CULPRIT
                'Zend\Loader\StandardAutoloader' => array(
                    'namespaces' => array(
                        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
                    )
                )
            );
        }
    
        public function getConfig() {
            return include __DIR__ . '/config/module.config.php';
        }
    
        public function getServiceConfig() {
            
                return array(
                'factories' => array(
                    'BookList\Model\Bookmodel' => function($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $table = new Bookmodel($dbAdapter);
                        return $table;
                    },
                ),
            );
    
        }
    
    }
    ?>
    
  • 4. create a model file(module/BookList/src/BookList/Model/Bookmodel.php)
  • <?php
    //copy and paste
    namespace BookList\Model;
    
    use Zend\Db\TableGateway\TableGateway;
    use Zend\Db\Sql\Where;
    use Zend\Db\Sql\Sql;
    use Zend\Db\Adapter\Adapter;
    use Zend\Db\Sql\Expression;
    
    class Bookmodel {
    
        protected $Bookmodel;
    
        public function __construct(Adapter $adapter) {
            $this->adapter = $adapter;
        }
    
        public function insertdata($tbl, $data) {
    
            $sql = new Sql($this->adapter);
            $insert = $sql->insert($tbl);
    
            $insert->values($data);
            $statement = $sql->prepareStatementForSqlObject($insert);
            $results = $statement->execute()->getAffectedRows();
            if ($results) {
                return $this->adapter->getDriver()->getLastGeneratedValue();
            }
        }
    
        public function updatedata($tbl, $data, $wherecondition) {
    
            $sql = new Sql($this->adapter);
            $update = $sql->update();
            $update->table($tbl);
            $update->set($data);
            $update->where($wherecondition);
    
            $statement = $sql->prepareStatementForSqlObject($update);
            return $results = $statement->execute()->getAffectedRows();
            //$statement->execute()->getAffectedRows();
        }
    
        public function deletedata($tbl, $wherecondition) {
    
            $sql = new Sql($this->adapter);
            $delete = $sql->delete();
            $delete->from($tbl);
            $delete->where($wherecondition);
            $statement = $sql->prepareStatementForSqlObject($delete);
            return $results = $statement->execute()->getAffectedRows();
            //$statement->execute()->getAffectedRows();
        }
    
        public function getAll() {
    
    
            $sql = new Sql($this->adapter);
    
            $select = $sql->select()
                    ->from('album')
                    //->where(array('id' => 1))
                    ->columns(array('title' => 'id', 'artist'))
            ;
    
            $select->order('id DESC')->order('title ASC'); // produces 'id' DESC
    
            $selectString = $sql->getSqlStringForSqlObject($select);
            $stmt = $this->adapter->query($selectString);
            $result = $stmt->execute();
            return $result->getResource()->fetchAll();
        }
    
    }
    
    
    
    ?>
    
  • 5. call this model in controller(module/BookList/src/BookList/Controller/BookController.php)
  • <?php
    //copy and paste to access all property above create model class
    namespace BookList\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    use Zend\Http\Request;
    use Zend\Session\Container;
    
    class BookController extends AbstractActionController {
    
        protected $book;
        protected $sesscontainer ;
     
        private function getSessContainer()
        {
            if (!$this->sesscontainer) {
                $this->sesscontainer = new Container('zftutorial');
            }
            return $this->sesscontainer;
        }
    
        public function __construct() {
            
            $this->layout('book-list/book/book');
        }
    
        public function getbook() {
            if (!$this->book) {
                $sm = $this->getServiceLocator();
                $this->book = $sm->get('BookList\Model\Bookmodel');
            }
            return $this->book;
        }
    
        public function indexAction() {
            $this->layout('book-list/book/book');
            echo $this->getSessContainer()->hellogreeting;
            //$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
            //echo $this->getbook()->insertdata('album',array('title'=>'title'.time(),'artist'=>'artist'.time()));exit;
            //echo $status=$this->getbook()->updatedata('album',array('title'=>'1manish12','artist'=>'mansh'),array('id'=>15));
            //echo $status=$this->getbook()->deletedata('album',array('id'=>15));
            return new ViewModel(array(
                'albums' => $this->getbook()->getAll(),
            ));
        }
    
        public function addAction() {
            //$this->layout('book-list/book/book');
            if ($this->getRequest()->isPost()) {
                $postData = $this->getRequest()->getPost();
                
                //session set
                $this->getSessContainer()->hellogreeting = 'Hello '.$postData['title'];
    //                print_r($postData['title']);
    //            echo "manish";
                $id = $this->getbook()->insertdata('album', array('title' => $postData['title'], 'artist' => $postData['artist']));
                if ($id) {
                    $this->flashMessenger()->addMessage('added');
    
                    $this->redirect()->toRoute('booklist', array(
                        'controller' => 'book',
                        'action' => 'index'
                    ));
                }
            }
    
            //$viewModel = new ViewModel();
            //$viewModel->setTemplate('book-list/book/index');
    //$viewModel->setTerminal(true);
    //$this->layout('book-list/book/book');
            //return $viewModel;
            //$request = new Request();
    //echo $this->getEvent()->getRouteMatch()->getParam('action'); 
    //        echo $this->getEvent()->getRouteMatch()->getParam('controller');
            //if($this->getRequest()->getMethod() == 'POST'){
        }
    
        public function editAction() {
            //$this->layout('book-list/book/book');
            if ($this->getRequest()->isPost()) {
                $postData = $this->getRequest()->getPost();
    //                print_r($postData['title']);
    //            echo "manish";
                $id = $this->getbook()->insertdata('album', array('title' => $postData['title'], 'artist' => $postData['artist']));
                if ($id) {
                    $this->flashMessenger()->addMessage('added');
    
                    $this->redirect()->toRoute('booklist', array(
                        'controller' => 'book',
                        'action' => 'index'
                    ));
                }
            }
        }
    
        public function deleteAction() {
            $viewModel = new ViewModel();
            $viewModel->setTerminal(true);
            $id = $this->params()->fromQuery('id');
            //session unset
                $this->getSessContainer()->offsetUnset('hellogreeting');
            if (!empty($id)) {
                $status = $this->getbook()->deletedata('album', array('id' => $id));
                if ($status) {
                    $this->flashMessenger()->addMessage('deleted');
    
                    $this->redirect()->toRoute('booklist', array(
                        'controller' => 'book',
                        'action' => 'index'
                    ));
                    
                } else {
                    $this->redirect()->toRoute('booklist', array(
                        'controller' => 'book',
                        'action' => 'index'
                    ));
                    
                }
            }
            $this->redirect()->toRoute('booklist', array(
                        'controller' => 'book',
                        'action' => 'index'
                    ));
            exit;
        }
    
    }
    
    ?>
    

    create New module

  • 1. add module name in (/config/appliction.config.php)
  • 'modules' => array(
            'Application',
            'BookList',
            'newmodulName'//new module add here
        )
    

    2. create folder structure for module
    3. add code in newmodulName config file (module/newmodulName/config/module.config.php)

    
    
    <?php
    //copy and paste code
    return array(
        'router' => array(
            'routes' => array(
                //set here for url like newmodule
                'newmodule' => array(
                    'type' => 'segment',
                    'options' => array(
            //new module controller name change here
                        'route'    => '/newmodulNamecontrollername[/][:action][/:id]',
                        'constraints'=>array(
                            'action'=>'[a-zA-Z][a-zA-Z0-9_-]*',
                            'id'=>'[0-9]+'
                        ),
                        'defaults' => array(
                            'controller' => 'newmodulName\Controller\newmodulNamecontrollername',
                            'action'     => 'index',
                        ),
                    ),
                ),
                
            ),
        ),
        'controllers' => array(
            'invokables' => array(
                'newmodulName\Controller\Book' => 'newmodulName\Controller\newmodulNamecontrollername'
            ),
        ),
        'view_manager' => array(
            
            'template_path_stack' => array(
                'newmodulName'=>__DIR__ . '/../view',
            ),
        ),
    );
    ?>
    

Comments (1)

Leave a Comment

© 2014 Powered By