We are now ready to set up our controller. For zend-mvc, the controller is a class that is generally called {Controller name}Controller;
note that {Controller name}
must start with a capital letter. This class lives in a file called {Controller name}Controller.php
within the Controller subdirectory for the module; in our case that is module/Album/src/Controller/
. Each action is a public method within the controller class that is named {action name}Action
, where {action name}
should start with a lower case letter.
Conventions not strictly enforced : This is by convention. zend-mvc doesn't provide many restrictions on controllers other than that they must implement the Zend\Stdlib\Dispatchable
interface. The framework provides two abstract classes that do this for us: Zend\Mvc\Controller\AbstractActionController
and Zend\Mvc\Controller\AbstractRestfulController.
We'll be using the standard AbstractActionController
, but if you’re intending to write a RESTful web service, AbstractRestfulController may be useful.
Let’s go ahead and create our controller class in the file zf-tutorials/module/Album/src/Controller/AlbumController.php
:
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
public function indexAction()
{
}
public function addAction()
{
}
public function editAction()
{
}
public function deleteAction()
{
}
}
We have now set up the four actions that we want to use. They won't work yet until we set up the views. The URLs for each action are :
URL |
Method called |
http://zf-tutorial.localhost/album |
Album\Controller\AlbumController::indexAction |
http://zf-tutorial.localhost/album/add |
Album\Controller\AlbumController::addAction |
http://zf-tutorial.localhost/album/edit |
Album\Controller\AlbumController::editAction |
http://zf-tutorial.localhost/album/delete |
Album\Controller\AlbumController::deleteAction |
We now have a working router and the actions are set up for each page of our application.
It's time to build the view and the model layer.