我已经设置了一个自定义路由,我的 routes.php 目前看起来像这样:
<?php
$router = new Phalcon\Mvc\Router();
$router->add('athlete/{username}', array(
'controller' => 'athlete',
'action' => 'index',
));
return $router;
在我的 services.php 中,我正在设置路由器:
$di->set('router', function() {
return require __DIR__ . '/routes.php';
});
我的 AthleteController 的索引 Action 看起来像这样,出于测试目的,我将参数传递给视图 ( username
):
public function indexAction()
{
$username = $this->dispatcher->getParam('username');
if (!isset($username)) {
$user = Users::findFirstByUsername($this->auth->getIdentity()['username']);
} else {
$user = Users::findFirstByUsername($username);
if (!$user) {
$this->flash->error('Could not find this user');
}
}
$this->view->user = $user;
$this->view->username = $username;
$this->view->setTemplateBefore('public');
}
当我不使用自定义参数时,它可以工作,它可以找到用户并显示他的用户名。但是,当我使用例如 urlathlete/kerowan
时,我得到错误Action 'kerowan' was not found on handler 'athlete'
,据我了解,这意味着路由器设置不正确或没有捕获 URL。有任何想法吗?