Reputation: 11
This is my first class
<?php
namespace Config\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Config\Model\Config;
use Config\Form\ConfigForm;
class ConfigController extends AbstractActionController
{
protected $configTable;
public function indexAction()
{
$this->getSMTPConfigTable();
return new ViewModel(array(
'config' => $this->getConfigTable()->fetchAll(),
));
}
public function addAction()
{
$form = new ConfigForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$config = new Config();
$form->setInputFilter($config->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$config->exchangeArray($form->getData());
$this->getConfigTable()->saveConfig($config);
return $this->redirect()->toRoute('zfcadmin/config');
}
}
return array('form' => $form);
}
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('zfcadmin/config', array(
'action' => 'add'
));
}
try {
$config = $this->getConfigTable()->getConfig($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('zfcadmin/config', array(
'action' => 'index'
));
}
$form = new ConfigForm();
$form->bind($config);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($config->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getConfigTable()->saveConfig($form->getData());
return $this->redirect()->toRoute('zfcadmin/config');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
public function getConfigTable()
{
if (!$this->configTable) {
$sm = $this->getServiceLocator();
$this->configTable = $sm->get('Config\Model\ConfigTable');
}
return $this->configTable;
}
public function getSMTPConfigTable()
{
$pr=$this->getConfigTable()->fetchAll();
return $pr;
}
}
in another module class how am I do it? I have trying most ways but I am failing please help me.(sorry for my English ) I need $temp=new ConfigController(); $temp->getSMTPConfigTable();
Upvotes: 1
Views: 2888
Reputation: 5539
I first want to check if I understand your question correct. Reading the name and the content of the question I assume you ask about a way to get the ConfigTable (Model) in another controller or class of the module(s).
To understand how the object and services are managed in ZF2 I advice you to have a look at this ZF2 manual page:
http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html
If you pay attention to the section "Creating a ServiceLocator-aware class" of the page, you will find out that you can get access to any object registered with the service manager via the service manager itself. The service manager in its terms is injected in every object that implements the ServiceLocatorAwareInterface
or simply has method named setServiceLocator()
.
Having that clear, the ZF2's Zend\Mvc\Controller\AbstractActionController
implements this interface meaning you can get the service manager in the object and therefore any other object registered with it. If you have a look at the following method (from your Config\Controller
) there is a good example of getting the model from the service manager:
public function getConfigTable()
{
if (!$this->configTable) {
$sm = $this->getServiceLocator();
$this->configTable = $sm->get('Config\Model\ConfigTable'); // <-- HERE!
}
return $this->configTable;
}
So, in every class that is created via the ServiceManager
and has the setServiceLocator()
(and optionaly getServiceLocator()
) methods you can access your model in the same way:
$sm = $this->getServiceLocator();
// or
$sm = $this->serviceLocator; // or whatever attribute you have set it to.
$configTable = $sm->get('Config\Model\ConfigTable');
About how to call the fetchAll()
method of your model I leave this decision to you where to do it.
Upvotes: 4
Reputation: 16455
This is exactly the part, where you use the ServiceManager
You would register your TableGateway
(or whatever it is you're using as "Model Table") to the ServiceManager
like this:
//Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'my-model-gateway' => function($sm) {
$gw = new MyTableGateway($sm->get('Zend\Db\Adapter\Adapter'));
return $gw;
}
)
}
}
This is only a showcase-scenario - it won't work copy-paste like this for you - i'm not getting clever out of your code, but you can use this simple example to know what is done. I register a service called my-model-gateway
and this service creates a class called MyTableGateway
. This class will be injected the default Zend\Db\Adapter\Adapter
via Constructor-Injection.
Using this, you would access this Service from ANY Controller of ANY Module like this:
$gw = $this->getServiceLocator()->get('my-model-gateway');
Hope this can get you started.
Upvotes: 2
Reputation: 736
You will need a contructor for that. Try with this:
class ConfigController {
function __construct() {
}
}
Even if it's empty, you will get the instance of the class and then you will be able to access to all parameters. Maybe you will need to create an init too for the general setting of the class.
Upvotes: 0