Redirect Zend Framework 2重定向不工作
我遇到ZF2无法从控制器重定向的问题。当我调用url xyz.dev/event时,它会重定向到routes中定义的my home(“/”)。当我调用xyz.dev/event/1(现在设置了id)时,它也会重定向到home,但这并没有发生。我得到了错误Redirect Zend Framework 2重定向不工作,redirect,zend-framework2,Redirect,Zend Framework2,我遇到ZF2无法从控制器重定向的问题。当我调用url xyz.dev/event时,它会重定向到routes中定义的my home(“/”)。当我调用xyz.dev/event/1(现在设置了id)时,它也会重定向到home,但这并没有发生。我得到了错误 Url plugin requires that controller event compose a router; none found 我的代码在这里: namespace Event\Controller; use Zend\Mvc\
Url plugin requires that controller event compose a router; none found
我的代码在这里:
namespace Event\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Router\SimpleRouteStack;
use Event\Model\EventTable;
use Zend\Mvc\Router\Http\Segment;
use Zend\Mvc\MvcEvent;
class IndexController extends AbstractActionController {
protected $eventsTable;
public $event;
public $_event;
public function indexAction() {
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute("home");
}
$this->event = $this->getEventsTable()->getEvent($id);
// if ($this->event->end < date("Y-m-d")) {
return $this->redirect()->toRoute('home');
// }
return new ViewModel(array("event" => $this->event));
}
public function getEventsTable() {
if (!$this->eventsTable) {
$sm = $this->getServiceLocator();
$this->eventsTable = $sm->get("Event\Model\EventTable");
}
return $this->eventsTable;
}
}
工作正常,第二个不是。。。有什么想法吗?
非常感谢。
Alex检查module.config文件路由部分,并检查哪个路由条件正确,如下所示:
'home' => array(
'type' => 'segment',
'options' => array(
'route' => '/home[/:action][/:id][/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
),
'defaults' => array(
'controller' => 'Home\Controller\Home',
'action' => 'index',
),
),
),
您可能忘记在路由参数中提及可选参数(id)
我希望这能奏效
'home' => array(
'type' => 'segment',
'options' => array(
'route' => '/home[/:action][/:id][/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
),
'defaults' => array(
'controller' => 'Home\Controller\Home',
'action' => 'index',
),
),
),