getServicelocator()
犯错误。所以它需要另一种方式。并扩展AbstractTableGateway
或ServiceLocatorAwareInterface
有错误。
工厂实现将帮助 Controller 获取对象。
*用户示例代码将类似于专辑。
1) 工厂类 (RegisterControllerFactory.php) * 在控制器中复制函数 createUser
namespace Users\Controller\Factory;
use Users\Controller\RegisterController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
class RegisterControllerFactory
{
public function __invoke($serviceLocator)
{
$sm = $serviceLocator;
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new \Users\Model\User);
$tableGateway = new \Zend\Db\TableGateway\TableGateway('user' /* table name */, $dbAdapter, null, $resultSetPrototype);
$user = new \Users\Model\User();
$userTable = new \Users\Model\UserTable($tableGateway);
$controller = new RegisterController($userTable, $serviceLocator );
return $controller;
}
}
2)控制器(RegisterController)命名空间用户\控制器;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;
use Users\Model\User;
use Users\Model\UserTable;
use Zend\ServiceManager\ServiceLocatorInterface;
class RegisterController extends AbstractActionController
{
protected $userTable;
protected $serviceManager;
public function __construct(UserTable $userTable, ServiceLocatorInterface $serviceManager)
{
$this->userTable = $userTable;
$this->serviceManager = $serviceManager;
}
public function indexAction()
{
$form = new RegisterForm();
$viewModel = new ViewModel(array('form' => $form));
return $viewModel;
}
public function processAction()
{
if (!$this->request->isPost()) {
return $this->redirect()->toRoute(NULL , array(
'controller' => 'register',
'action' => 'index'
));
}
$post = $this->request->getPost();
$form = new RegisterForm();
$inputFilter = new RegisterFilter();
$form->setInputFilter($inputFilter);
$form->setData($post);
if (!$form->isValid()) {
$model = new ViewModel(array(
'error' => true,
'form' => $form,
));
$model->setTemplate('users/register/index');
return $model;
}
// Create user
$this->createUser($form->getData());
return $this->redirect()->toRoute(NULL , array(
'controller' => 'register',
'action' => 'confirm'
));
}
public function confirmAction()
{
$viewModel = new ViewModel();
return $viewModel;
}
protected function createUser(array $data)
{ /*able to delete or modify */
$sm = $this->serviceManager;
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new \Users\Model\User);
$tableGateway = new \Zend\Db\TableGateway\TableGateway('user' /* table name */, $dbAdapter, null, $resultSetPrototype);
$user = new User();
$user->exchangeArray($data);
$userTable = new UserTable($tableGateway);
$userTable->saveUser($user);
return true;
}
}
3) 模块.config.php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Index' => 'Users\Controller\IndexController',
'Users\Controller\login' => 'Users\Controller\LoginController',
//delete 'Users\Controller\Register'
),
'factories' => array(
'Users\Controller\Register' => 'Users\Controller\Factory\RegisterControllerFactory',
),
),