1
votes

I'm looking at a module called Wall that is configured:

return array(
    'router' => array(
        'routes' => array(
            'wall' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '/api/wall[/:id]',
                    'constraints' => array(
                        'id' => '\w+'
                    ),
                    'defaults' => array(
                        'controller' => 'Wall\Controller\Index'
                    ),
                ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Wall\Controller\Index' => 'Wall\Controller\IndexController',
        ),
    ),

So the 'defaults' member of the 'router' array is set to the value Wall\Controller\Index. So Wall\Controller\Index I take to be a namespace but I really don't understand the meaning of why its set that way. The controller is defined in IndexController.php:

<?php
namespace Wall\Controller;

use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;

class IndexController extends AbstractRestfulController
{
    protected $usersTable;

    public function get($username)
    {
        $usersTable = $this->getUsersTable();
        $userData = $usersTable->getByUsername($username);
        $wallData = $userData->getArrayCopy();

        if ($userData !== false) {
            return new JsonModel($wallData);
        } else {
            throw new \Exception('User not found', 404);
        }
    }
}

So the only method in the controller that takes a parameter is get so I have to say that is what is being called when one visits /wall/tusername but it is not clear to me how the route works. So the defaults for the route wall is set to "Wall\Controller\Index" what does that mean? Does it mean anything that 'action' is not declared in 'defaults'? What is the behavior if 'action' is not declared?

Thank you for posting.

1

1 Answers

2
votes

For REST-full Application there will be no actions defined, You can see that it has extended AbstractRestfulController and not AbstractActionController

RestfulController will work like this:

When you call the URL with POST parameters then it will map to Create method, 
If you call with GET parameters it will map to GET or GETList method 
same as Delete->Delete and PUT -> Update

So for this application route will just check which controller needs to be activated and mapping of function will be done by above process.

Hope that Helps