YAF Framework Integration Zendframework2 in PHP

Source: Internet
Author: User


The PHP framework YAF integrated ZENDFRAMEWORK2, ZF2 ORM can be used as a stand-alone module in Yaf, and ZF2 composer Service Manger Cachestorage can be integrated into the YAF.

One: public\index.php join composer

ChDir (DirName (__dir__));

//Decline static file requests back to the PHP built-in webserver

if (php_sapi_name () = = ' Cli-server ' && ; Is_file (__dir__. Parse_url ($_server[' Request_uri ', Php_url_path))) {return

false;

}

Setup autoloading

require ' init_autoloader.php ';

Define path to application directory

Define ("App_path", DirName (__dir__));

Create application, bootstrap, and run

$app = new Yaf_application (App_path. "/conf/application.ini");

$app->bootstrap ()->run ();


root directory Storage init_autoloader.php

II: Import ZF2 Module components

VENDOR\ZF2 See the end of the page download package

Three: Change the bootstrap configuration file

<?php   use zend\servicemanager\servicemanager;

use zend\mvc\service\servicemanagerconfig;

use zend\modulemanager\listener\configlistener;

use zend\modulemanager\listener\listeneroptions;

use zend\modulemanager\moduleevent;   class bootstrap extends yaf_bootstrap_abstract {  Public function _

Initconfig ()  {$config  = yaf_application::app ()->getconfig ();

Yaf_registry::set ("config",  $config); }   Public function _initservicemanager ()  {$configuration  = require app_path

 .  '/conf/application.config.php '; $smConfig  = isset ($configuration [' Service_manager '])  ?  $configuration [' Service_manager ']

 : array ();

$serviceManager  = new servicemanager (New servicemanagerconfig ($smConfig));

$serviceManager->setservice (' applicationconfig ',  $configuration);   $configListener  =&nbsP;new configlistener (New listeneroptions ($configuration [' module_listener_options ']));  // if not found cache, merge config if  (! $configListener->

Getmergedconfig (false)   $configListener->onmergeconfig (new moduleevent);  // if enabled, update the config cache if  ($configListener-> GetOptions ()->getconfigcacheenabled ()  &&!file_exists ($configListener->getoptions ()->

Getconfigcachefile ())  {//echo  "Debug";

$configFile  =  $configListener->getoptions ()->getconfigcachefile (); $content  =  "<?php\nreturn "  . var_export ($configListener->getmergedconfig (False

),  1  .  ';

File_put_contents ($configFile,  $content);

}   $serviceManager->setservice (' config ',  $configListener->getmergedconfig (false));

  Yaf_registry::set (' ServiceManager ',  $serviceManager); }

  Public function _initsessionmanager ()  {yaf_registry::get (' ServiceManager ')->get (' Zend\

Session\sessionmanager '); }   Public function _initplugin (yaf_dispatcher  $dispatcher)  {$user  = new

 userplugin ();

$dispatcher->registerplugin ($user); }  }

Four: MVC test

<?php   use zend\session\container as sessioncontainer;

use zend\db\tablegateway\tablegateway;   class indexcontroller extends yaf_controller_abstract {  Public function

 indexaction ()  {  $adapter  =  $this->getdbadapter ();

  $table  = new tablegateway (' Zt_user ',  $adapter);

  $entities  =  $table->select ();

foreach  ($entities  as  $entity)  {var_dump ($entity->username);

}   $cache  =  $this->getstorage ();

$cache->setitem (' Cache ',  ' cachedata ');

  echo  $cache->getitem (' cache ');

$this->getlogger ()->alert (' Log ');

  $this->getview ()->assign ("Content",  "Hello world"); }  /** * db adapter *  @return  \zend\db\adapter\adapter * * * PUBLIC&NBSP;FUNCTION&NBSP;GETDBA Dapter ()  {return yaf_registry::get (' ServiceManager ')->Get (' Zend\db\adapter\adapter '); }  /** * storage *  @return  \zend\cache\storage\storageinterface * * * protected function&nbsp ; GetStorage ()  {return yaf_registry::get (' ServiceManager ')->get (' zend\cache\storage\

Storageinterface '); }  /** * logger *  @return  \zend\log\zend\log\logger * * * Protected function getlogger ()

 {return yaf_registry::get (' ServiceManager ')->get (' Zend\log\logger '); }  }


So you access public index.php will output Hello word words

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.