Zend Framework 1中的自定义路由

时间:2015-04-02 14:57:57

标签: zend-framework

我尝试在Zend Framework 1中配置其他路由,如下所示。

$route = new Zend_Controller_Router_Route (
    'test/route/',
    array(
        'module' => 'storefront',
        'controller' => 'index',
        'action' => 'index' 
    )
); 
$router->addRoute('customRoute', $route);

然后我尝试测试路线并在浏览器中输入以下URL:

http://localhost:8888/selfstudy/zend/storefront/public/storefront/test/route/

我总是得到一个错误页面说:" 404未找到。未找到。在此服务器上找不到请求的URL /index.php。"

我在自举过程中加载的application.ini如下所示:

[bootstrap]
Autoloadernamespaces[] = "Zend_"
Autoloadernamespaces[] = "SF_"

phpsettings.display_errors = 0
phpsettings.error_reporting = 8191
phpsettings.date.timezone = "Europe/London"

bootstrap.path = APPLICATION_PATH"/bootstrap/Bootstrap.php"

resources.frontcontroller.moduledirectory = APPLICATION_PATH"/modules"
resources.frontcontroller.defaultmodule = "storefront"
resources.frontcontroller.params.prefixDefaultModule = true
resources.frontcontroller.throwerrors = false

resources.view = ""

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "main"

[production : bootstrap]

[development : bootstrap]
phpsettings.display_errors = 1
resources.frontcontroller.throwerrors = true

[test : bootstrap]

对于调试我转储了所有路由:

array (size=2)
  'default' => 
    object(Zend_Controller_Router_Route_Module)[46]
      protected '_defaults' => 
        array (size=3)
          'controller' => string 'index' (length=5)
          'action' => string 'index' (length=5)
          'module' => string 'storefront' (length=10)
      protected '_values' => 
        array (size=0)
          empty
      protected '_moduleValid' => boolean false
      protected '_keysSet' => boolean true
      protected '_moduleKey' => string 'module' (length=6)
      protected '_controllerKey' => string 'controller' (length=10)
      protected '_actionKey' => string 'action' (length=6)
      protected '_dispatcher' => 
        object(Zend_Controller_Dispatcher_Standard)[40]
          protected '_curDirectory' => string '/Applications/MAMP/htdocs/selfstudy/zend/storefront/application/modules/storefront/controllers' (length=94)
          protected '_curModule' => string 'storefront' (length=10)
          protected '_controllerDirectory' => 
            array (size=1)
              ...
          protected '_defaultAction' => string 'index' (length=5)
          protected '_defaultController' => string 'index' (length=5)
          protected '_defaultModule' => string 'storefront' (length=10)
          protected '_frontController' => null
          protected '_invokeParams' => 
            array (size=3)
              ...
          protected '_pathDelimiter' => string '_' (length=1)
          protected '_response' => 
            object(Zend_Controller_Response_Http)[45]
              ...
          protected '_wordDelimiter' => 
            array (size=2)
              ...
      protected '_request' => 
        object(Zend_Controller_Request_Http)[44]
          protected '_paramSources' => 
            array (size=2)
              ...
          protected '_requestUri' => string '/selfstudy/zend/storefront/public/index.php' (length=43)
          protected '_baseUrl' => string '/selfstudy/zend/storefront/public/index.php' (length=43)
          protected '_basePath' => null
          protected '_pathInfo' => string '' (length=0)
          protected '_params' => 
            array (size=3)
              ...
          protected '_rawBody' => null
          protected '_aliases' => 
            array (size=0)
              ...
          protected '_dispatched' => boolean true
          protected '_module' => string 'storefront' (length=10)
          protected '_moduleKey' => string 'module' (length=6)
          protected '_controller' => string 'index' (length=5)
          protected '_controllerKey' => string 'controller' (length=10)
          protected '_action' => string 'index' (length=5)
          protected '_actionKey' => string 'action' (length=6)
      protected '_isAbstract' => boolean false
      protected '_matchedPath' => null
  'customRoute' => 
    object(Zend_Controller_Router_Route)[5]
      protected '_translator' => null
      protected '_locale' => null
      protected '_isTranslated' => boolean false
      protected '_translatable' => 
        array (size=0)
          empty
      protected '_urlVariable' => string ':' (length=1)
      protected '_urlDelimiter' => string '/' (length=1)
      protected '_regexDelimiter' => string '#' (length=1)
      protected '_defaultRegex' => null
      protected '_variables' => 
        array (size=0)
          empty
      protected '_parts' => 
        array (size=2)
          0 => string 'test' (length=4)
          1 => string 'route' (length=5)
      protected '_defaults' => 
        array (size=3)
          'module' => string 'storefront' (length=10)
          'controller' => string 'index' (length=5)
          'action' => string 'index' (length=5)
      protected '_requirements' => 
        array (size=0)
          empty
      protected '_values' => 
        array (size=0)
          empty
      protected '_wildcardData' => 
        array (size=0)
          empty
      protected '_staticCount' => int 2
      protected '_isAbstract' => boolean false
      protected '_matchedPath' => null

我真的不知道为什么它没有按预期工作。通常这应该是一件容易的事。非常感谢任何建议!

更新

由于任何原因,Zend Framework重定向URL的请求

http://localhost:8888/selfstudy/zend/storefront/public/storefront/index/index/ 

http://localhost:8888/index.php

可能是因为我的application.ini中有错误/缺少配置?

更新2:mod_rewrite log

::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] add path info postfix: /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront -> /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront/test/route
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] strip per-dir prefix: /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront/test/route -> storefront/test/route
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] applying pattern '^.*$' to uri 'storefront/test/route'
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (4) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] RewriteCond: input='/Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront' pattern='-s' => not-matched
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (4) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] RewriteCond: input='/Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront' pattern='-l' => not-matched
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (4) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] RewriteCond: input='/Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront' pattern='-d' => not-matched
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] add path info postfix: /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront -> /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront/test/route
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] strip per-dir prefix: /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront/test/route -> storefront/test/route
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] applying pattern '^(.*)$' to uri 'storefront/test/route'
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (4) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] RewriteCond: input='/selfstudy/zend/storefront/public/storefront/test/route::storefront/test/route' pattern='^(/.+)(.+)::\\2$' => matched
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (5) setting env variable 'BASE' to '/selfstudy/zend/storefront/public/'
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] add path info postfix: /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront -> /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront/test/route
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] strip per-dir prefix: /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/storefront/test/route -> storefront/test/route
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] applying pattern '^(.*)$' to uri 'storefront/test/route'
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (2) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] rewrite 'storefront/test/route' -> '/selfstudy/zend/storefront/public/index.php'
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#10288b0a0/initial] (1) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] internal redirect with /selfstudy/zend/storefront/public/index.php [INTERNAL REDIRECT]
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#102896c80/initial/redir#1] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] strip per-dir prefix: /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/index.php -> index.php
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#102896c80/initial/redir#1] (3) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] applying pattern '^.*$' to uri 'index.php'
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#102896c80/initial/redir#1] (4) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] RewriteCond: input='/Applications/MAMP/htdocs/selfstudy/zend/storefront/public/index.php' pattern='-s' => matched
::1 - - [04/Apr/2015:20:18:52 +0200] [localhost/sid#101001730][rid#102896c80/initial/redir#1] (1) [perdir /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/] pass through /Applications/MAMP/htdocs/selfstudy/zend/storefront/public/index.php

2 个答案:

答案 0 :(得分:0)

如果没有记错的话,这是你的地址不符合你的路线

对于此网址.../public/storefront/test/route/,请尝试以下路线:

$route = new Zend_Controller_Router_Route (
    'storefront/test/route/', // add 'storefront/'
    array(
        'module' => 'storefront',
        'controller' => 'index',
        'action' => 'index' 
    )
); 

或此路线

$route = new Zend_Controller_Router_Route (
    'test/route/',
    array(
        'module' => 'storefront',
        'controller' => 'index',
        'action' => 'index' 
    )
); 

试试这个网址:

`.../public/test/route/`  /* without 'storefront/' */

<强>更新
我的.htaccess很基本:

SetEnv APPLICATION_ENV development

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

答案 1 :(得分:0)

我认为问题出在你的.htaccess中 将.htaccess与此内容一起使用:

  

RewriteEngine On

     

RewriteCond%{REQUEST_FILENAME} -s [OR]

     

RewriteCond%{REQUEST_FILENAME} -l [OR]

     

RewriteCond%{REQUEST_FILENAME} -d

     

RewriteRule ^。* $ - [NC,L]

     

RewriteCond%{REQUEST_URI} :: $ 1 ^(/。+)(。+):: \ 2 $

     

RewriteRule ^(。*)$ - [E = BASE:%1]

     

RewriteRule ^(。*)$%{ENV:BASE} index.php [NC,L]

** .htaccess是/ public目录中的隐藏文件