我正在开发一个zf2应用程序,并且遇到了一些路由问题。
我有一个控制器和15个视图。他们中的大多数工作正常,另一半,我得到以下例外:
执行期间发生错误;请稍后再试。
其他信息: 的Zend \的mvc \路由器\异常\ RuntimeException的
文件: /Users/<username>/Sites/zend/vendor/zendframework/zendframework/library/Zend/Mvc/Router/Http/TreeRouteStack.php:313 信息: 找不到名称“类别”的路线
这是控制器类:
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;
}
}
这是module.config.php
文件:
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',
),
),
);
所以我遇到的问题是:
最奇怪的是,在位置视图的错误中,例外情况为route with name "location" (singular) not found
。我不知道从哪里获取单词位置。如果我输出$model
变量,则会打印locations
。
任何人都可以对此有所了解吗?
非常感谢提前。
答案 0 :(得分: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',
),
),
),