-1

I'm working on a zf2 app, and am having some issues with routing.

I have one controller, and 15 views. The majority of them work fine, with the other half, I get the following exception:

An error occurred during execution; please try again later.

Additional information: Zend\Mvc\Router\Exception\RuntimeException

File: /Users/<username>/Sites/zend/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/TreeRouteStack.php:313 Message: Route with name "categories" not found

Here's the controller class:

namespace Stock\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Json\Json;
use Application\Model\Application;

class StockController extends AbstractActionController
{
    private $apiRoutes = array(
            'categories'    => 'catalog/category',
            'certificates'  => 'catalog/certificate',
            'constructions' => 'catalog/construction',
            'colours'       => 'catalog/color',
            'departments'   => 'catalog/department',
            'designers'     => 'catalog/designer',
            'designs'       => 'catalog/design',
            'discounts'     => 'sales/discount',
            'locations'     => 'catalog/location',
            'materials'     => 'catalog/material',
            'patterns'      => 'catalog/pattern',
            'products'      => 'catalog/product',
            'ranges'        => 'catalog/range',
            'shapes'        => 'catalog/shape',
            'suppliers'     => 'catalog/supplier' 
        );

    public function indexAction()
    {
        $model = $this->params()->fromRoute('view') ? $this->params()->fromRoute('view') : 'index';

        $form = null;
        $dataObj = null;
        $itemId = null;

        $modelForm = 'Stock\Form\\' . ucfirst($model) . 'Form';

        if(class_exists($modelForm)){
            $form = new $modelForm();
            $API = new Application();

            $itemId = $this->params()->fromRoute('id') ? $this->params()->fromRoute('id') : null;

            $dataObj = $API->get($this->apiRoutes[$model]);

            if($dataObj){
                foreach($dataObj as $data){

                    $vars = get_object_vars($data);

                    if($data->{key($vars)} == $itemId){
                        $form->setData($vars);
                    }
                }
            }

            $formControllerResponse = $this->validate($form);

            //if response is an object, it contains form errors, so pass client-side for JS to handle.
            if($formControllerResponse->valid){
                //update magento...
                $data = $form->getData();

                $API->put($this->apiRoutes[$model], $data);
            }

            if(isset($formControllerResponse->response)) return $formControllerResponse->response;
        }

        $view = new ViewModel(array('form' => $form, 'dataObj' => $dataObj, 'itemId' => $itemId));
        $view->setTemplate("stock/stock/$model");
        return $view; 
    }

    public function validate($form)
    {
        $filterClass = get_class($form) . 'Filter';
        $request = $this->getRequest();
        $response = $this->getResponse();

        $return = new \stdClass();
        $return->valid = false;

        $message = array('errors' => false);

        if($request->isXmlHttpRequest() && $request->isPost()){

            $form->setInputFilter(new $filterClass());
            $form->setData($request->getPost());

            if(!$form->isValid()){
                $errors = $form->getMessages();
                foreach($errors as $key => $error){
                    $message['errors'][$key] = implode($error, '; ');
                }
            }

            else {
                $return->valid = true;
            }

            $response->setContent(Json::encode($message));

            $return->response = $response;
        }

        if($request->isPost()){
            $form->setInputFilter(new $filterClass());
            $form->setData($request->getPost());

            if($form->isValid()){
                $return->valid = true;
            }
        }

        return $return;
    }
}

And here's the module.config.php file:

return array(
    'controllers' => array(
        'invokables' => array(
            'Stock\Controller\Stock' => 'Stock\Controller\StockController',
            'Stock\Controller\Media' => 'Stock\Controller\MediaController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'stock' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/stock[/][/:view][/:id]',
                    'constraints' => array(
                        'view'    => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'      => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Stock\Controller\Stock',
                        'action'     => 'index',
                    ),
                ),
            ),
            'media' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/stock/media[/]',
                    'constraints' => array(
                        'id'            => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Stock\Controller\Media',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'stock' => __DIR__ . '/../view',
        ),
    ),
);

So the views I'm having issues with are:

  • categories;
  • certificates;
  • colours;
  • designs;
  • discounts;
  • locations;

Most bizarrely, in the error for the locations view, the exception says route with name "location" (singular) not found. I don't know where its getting the word location from. If I output the $model variable it prints locations.

Can anyone shed any light on this?

Many thanks in advance.

4

1 回答 1

0

您需要为您遇到错误的页面定义路由,例如,我将为类别创建 1 作为示例(您可以根据您的情况对其进行修改)

      'categories' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/stock/categories[/]',
                'constraints' => array(
                    'id'            => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Stock\Controller\Categories',\\ assuming you need a controller for categories else you have to change below line for its action
                    'action'     => 'index',
                ),
            ),
        ),
于 2013-08-12T14:29:25.483 回答