下面是我的多语言路由器结构
$router->add('/{language:[a-z]{2}+}/?', array(
'controller' => 'index',
'action' => 'index',
));
$router->add('/{language:[a-z]{2}+}/:controller', array(
'controller' => 2
));
$router->add('/{language:[a-z]{2}+}/:controller/:action', array(
'controller' => 2,
'action' => 3,
));
$router->add('/{language:[a-z]{2}+}/:controller/:action/:params', array(
'controller' => 2,
'action' => 3,
"params" => 4,
));
和我的控制器
public function viewAction($slug=null)
{
if($slug==null){
$slug=$this->auth->getId();
}
$parameters = array(
"name" => $slug,
"id" => $slug
);
//Casting Types
$types = array(
"name" => Column::BIND_PARAM_STR,
"id" => Column::BIND_PARAM_INT
);
$user = Users::findFirst(array(
"id= :id: OR name = :name:",
"bind" => $parameters,
"bindTypes" => $types
));
if (empty($user)) {
return $this->dispatcher->forward(array(
'controller' => 'error','action' => 'show404'
));
}
$this->view->user = $user;
}
#0 [internal function]: PDOStatement->execute()
#1 [internal function]: Phalcon\Db\Adapter\Pdo->executePrepared(Object(PDOStatement), Array, Array)
#2 [internal function]: Phalcon\Db\Adapter\Pdo->query('SELECT "users"....', Array, Array)
#3 [internal function]: Phalcon\Mvc\Model\Query->_executeSelect(Array, Array, Array)
#4 [internal function]: Phalcon\Mvc\Model\Query->execute(Array, Array)
#5 C:\xampp\htdocs\tuteer\app\controllers\UsersController.php(62): Phalcon\Mvc\Model::findFirst(Array)
#6 [internal function]: Vokuro\Controllers\UsersController->viewAction('zh')
#7 [internal function]: Phalcon\Dispatcher->dispatch()
#8 C:\xampp\htdocs\tuteer\public\index.php(27): Phalcon\Mvc\Application->handle()
#9 {main}
如果我使用网址http://localhost/tuteer/zh/users/view/1
,则返回正确的响应,
但是如果我使用url http://localhost/tuteer/zh/users/view
,它将zh作为我的默认参数并且等于http://localhost/tuteer/zh/users/view/zh
,是否可以对路由器结构做些什么?
答案 0 :(得分:1)
$di->set('router', function() {
$router = new Phalcon\Mvc\Router();
$router->removeExtraSlashes(true);
$router->add('/{lang:[a-z]{2}}/:controller/:action/:params', array(
'controller' => 2,
'action' => 3,
'params' => 4,
));
$router->add('/{lang:[a-z]{2}}/:controller/:action', array(
'controller' => 2,
'action' => 3,
));
$router->add('/{lang:[a-z]{2}}/:controller', array(
'controller' => 2,
'action' => 'index',
));
$router->add('/{lang:[a-z]{2}}', array(
'controller' => 'index',
'action' => 'index',
));
return $router;
});
答案 1 :(得分:0)
问题的临时解决方法:
$lang = $langParam = $dispatcher->getParam('language');
if(!empty($langParam)){
if(count($dispatcher->getParams())==1){
$dispatcher->setParams(array());
}
}
但是我认为这种方法不够好,因为在你调用调度程序之后,你必须设置你的语言参数。等待更好的答案