【问题标题】:Url rewriting not working in zend framework 2URL重写在zend框架2中不起作用
【发布时间】:2013-08-27 10:07:25
【问题描述】:

我正在根据Zend quick start 中提供的文档设置 zend 框架 2 项目

我已经下载了骨架应用程序,它正确显示了“欢迎使用zend framework”主页。根据文档中的建议,我尝试访问 myproject.localhost/1234 url,但它没有在项目范围内显示 404 错误,但是在没有导航栏、css 的情况下打开了“未找到”页面(即 url 重写不起作用)。我的机器上没有安装 IIS,只有 wamp 服务器,所以理想情况下它应该可以工作。

谁能指导我解决这个错误。我也创建了虚拟主机

【问题讨论】:

  • 您是否检查过域 myproject.localhost 指向 127.0.0.1?

标签: php zend-framework zend-framework2


【解决方案1】:

因为您试图通过请求 myproject.local/1234 来解决 未配置的路由,所以如果您想将 1234 作为参数传递给您的索引控制器(主路由),请设置从this line 请求的路由如下:

'home' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
    'route'    => '/[:myparam]',
    'constraints' => array(
        'id' => '\d*',
    ),
    'defaults' => array(
        'controller' => 'Application\Controller\Index',
        'action'     => 'index',
        ),
    ),
),

现在您可以像这样在 IndexController 中获取 myparam:

public function indexAction()
{
    $myparam = (int) $this->params()->fromRoute('myparam', 0);
    var_dump($myparam); // Prints 123 on myproject.localhost/123 and 0 on myproject.localhost
    exit; // Exit for just simply show the logic. Don't use exit in controller like this.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多