1
votes

I made a Zend Framework 3 MVC application. I don't want a default router. My one controller RESTFUL and only returning JSON. I want to remove the default IndexController. I want / to just give a 404 error. I'd prefer not to call any route 'home' but will do that if necessary.

If I make my route config look like this:

'router' => [
    'routes' => [
        'myRoute' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/myThing[/:action]',
                'defaults' => [
                    'controller' => Controller\MyThingController::class,
                    'action'     => 'index',
                ],
            ],
        ],
    ],
],

I get the following exception when I connect to a route that worked when I kept the default index controller in my browser:

Fatal error: Uncaught Zend\Router\Exception\RuntimeException: Route with name "home" not found in /var/www/vendor/zendframework/zend-router/src/Http/TreeRouteStack.php on line 354

If I change 'myRoute' => [ to 'home' => [ It renders the default layout instead of the Json rendered by JsonViewModel.

2

2 Answers

0
votes

I just put an IndexController with a default route that renders the defautl 404 page for now. I'm going to make it return JSON at some point when I figure out how.

class IndexController extends AbstractRestfulController
{
    public function indexAction()
    {
        $this->response->setStatusCode(Response::STATUS_CODE_404);
    }
}
0
votes

I'm not sure if this is what your after but to return a json response from your controller use.

In your module config:

'view_manager' => array(
    'strategies' => array(
       'ViewJsonStrategy',
    ),
},

and in your controller:

use Zend\View\Model\JsonModel;
// ...
public function indexAction()
{
    $this->response->setStatusCode(Response::STATUS_CODE_404);
    $view = new JsonModel();
    $view->jsonVariable = 'someValue';
    return $view;
}

This will return a json response.

Hope this helps.