简体   繁体   English

Phalcon路线无效

[英]Phalcon route doesn't work

My phalcon app has worked fine with standard MVC route convention. 我的Phalcon应用程序在标准MVC路由约定下运行良好。 However, I want to handle some variable via URL, then I have a route: 但是,我想通过URL处理一些变量,然后有了一条路由:

$router = new \Phalcon\Mvc\Router();
$router->add("/timesheet/some/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::some");
$router->add("/timesheet/getreport/{type:[a-z]}/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::getreport");
$router->addPost("/user/auth", "User::auth");

return $router;
  1. The first route (timesheet/some) worked fine, I can access to "year", "month" variable using $year = $this->dispatcher->getParam("year"); 第一条路线(时间表/某条路线)运行良好,我可以使用$year = $this->dispatcher->getParam("year");访问“ year”,“ month”变量$year = $this->dispatcher->getParam("year"); , however the second route (timesheet/getreport) doesn't work. ,但是第二条路线(时间表/ getreport)不起作用。 In this case, $year = $this->dispatcher->getParam("year"); 在这种情况下, $year = $this->dispatcher->getParam("year"); return null. 返回null。

  2. If I changed to 如果我改为

    $router = new \\Phalcon\\Mvc\\Router(false); $ router = new \\ Phalcon \\ Mvc \\ Router(false);

    $router->add("/:controller/:action", array( "controller" => 1, "action" => 2, )); $ router-> add(“ /:controller /:action”,array(“ controller” => 1,“ action” => 2,));

    $router->add("/timesheet/some/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::some"); $ router-> add(“ / timesheet / some / {year:[0-9] +} / {month:[0-9] {2}} / {day:[0-9] {2}}”, “时间表::一些”); $router->addPost("/timesheet/getreport/{type:[az]}/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::getreport"); $ router-> addPost(“ / timesheet / getreport / {type:[az]} / {year:[0-9] +} / {month:[0-9] {2}} / {day:[0- 9] {2}}“,“时间表:: getreport”); $router->addPost("/user/auth", "User::auth"); $ router-> addPost(“ / user / auth”,“ User :: auth”);
    return $router; 返回$ router;

every request will be routed to index/index. 每个请求将被路由到索引/索引。 My project URL is localhost/fpas, and I already try both route /fpas/timesheet/some and /timesheet/some but it always redirect to index/index. 我的项目URL是localhost / fpas,我已经尝试了/ fpas / timesheet / some和/ timesheet / some路由,但是它总是重定向到index / index。 What's wrong with it? 它出什么问题了? (security/auth is commented out, so it's not result from authentication). (安全性/身份验证已被注释掉,因此它不是身份验证的结果)。

  1. From my understand, the default route, $router = new \\Phalcon\\Mvc\\Router(); 据我了解,默认路由$ router = new \\ Phalcon \\ Mvc \\ Router(); only allow you to follow MVC convention, while $router = new \\Phalcon\\Mvc\\Router(false); 只允许您遵循MVC约定,而$ router = new \\ Phalcon \\ Mvc \\ Router(false); do but you have to specific all routes for every controller/action. 但是您必须为每个控制器/操作指定所有路由。 Can I keep the convention for most of the action, while have specific rewrite routes for some action. 我可以为大多数操作保留约定,同时为某些操作指定特定的重写路径。 How can I do that? 我怎样才能做到这一点?

Thank you very much. 非常感谢你。

It works for me: 这个对我有用:

$router = new Phalcon\Mvc\Router();
$router->add("/", array(
    'controller' => 'index',
    'action' => 'setLanguage',
));

$router->add("/{language:[a-z]{2}}", array(
    'controller' => 'index',
    'action' => 'index',
    'language' => 1
));

this one get's default routing just with language in the beginning 这个开始的默认路由仅以语言开头

$router->add("/{language:[a-z]{2}}/:controller/:action", array(
    'controller' => 2,
    'action' => 3,
    'language' => 1
));

with default action "index" when it's not in url 不在网址中时使用默认操作“索引”

$router->add("/{language:[a-z]{2}}/:controller", array(
    'controller' => 2,
    'action' => 'index',
    'language' => 1
));

some other routes 其他路线

$router->add("/{language:[a-z]{2}}/:controller/:action/:params", array(
    'controller' => 2,
    'action' => 3,
    'language' => 1,
    'params' => 4
));

$router->add("/{language:[a-z]{2}}/question/add/{type}", array(
    'language' => 1,
    'controller' => 'question',
    'action' => 'add',
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM