URL视图助手中URL编码参数的问题

时间:2009-12-20 21:03:29

标签: php zend-framework

所以我的问题有点奇怪,只有当我离线测试应用程序时(在我的电脑上使用WampServer),相同的代码才能在线正常工作。

以下是我如何使用帮助器(仅示例):

<a href="<?php

echo $this->url(array('module' => 'admin',
                      'controller' => 'index',
                      'action' => 'approve-photo',
                      'id' => $this->escape($a->id),
                      'ret' => urlencode('admin/index/photos')),
                null,
                true);

                            ?>" class="blue">approve</a>

在线,此链接效果很好,它会转到类似于此的操作:

public function approvePhotoAction()
{
    $request = $this->getRequest();
    $photos = $this->_getTable('Photos');

    $dbTrans = false;

    try {

        $db = $this->_getDb();
        $dbTrans = $db->beginTransaction();

        $photos->edit($request->getParam('id'),
                      array('status' => 'approved'));

        $db->commit();

    } catch (Exception $e) {
        if (true === $dbTrans) {
            $db->rollBack();
        }
    }

    $this->_redirect(urldecode($request->getParam('ret')));
}

在网上,它会批准照片并重定向回URL中的“ret”参数URL(“admin / index / photos”)。

但是使用WampServer离线我点击链接并得到404错误,如下所示:

Not Found

The requested URL /public/admin/index/approve-photo/id/1/ret/admin/index/photos was not found on this server.

到底是什么?

我正在使用最新版本的WampServer(WampServer 2.0i [11/07/09])。其他一切都有效。

这是我的.htaccess文件,以防万一:

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

# Turn off magic quotes
#php_flag magic_quotes_gpc off

我正在使用虚拟主机在我的本地PC上测试ZF项目。以下是我添加虚拟主机的方法。

的httpd.conf:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName myproject
    DocumentRoot "C:\wamp\www\myproject"
</VirtualHost>

hosts文件:

127.0.0.1    myproject

任何帮助都会受到赞赏,因为这会使我在localhost上的测试和调试项目成为一场噩梦,几乎是不可能的任务。我必须在线上传所有内容以检查它是否有效:(

更新:

_redirect助手的源代码(内置在ZF助手中):

/**
 * Set redirect in response object
 *
 * @return void
 */
protected function _redirect($url)
{
    if ($this->getUseAbsoluteUri() && !preg_match('#^(https?|ftp)://#', $url)) {
        $host  = (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'');
        $proto = (isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=="off") ? 'https' : 'http';
        $port  = (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80);
        $uri   = $proto . '://' . $host;
        if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) {
            $uri .= ':' . $port;
        }
        $url = $uri . '/' . ltrim($url, '/');
    }
    $this->_redirectUrl = $url;
    $this->getResponse()->setRedirect($url, $this->getCode());
}

更新2:

脱机助手的输出:

/admin/index/approve-photo/id/1/ret/admin%252Findex%252Fphotos

和在线(相同):

/admin/index/approve-photo/id/1/ret/admin%252Findex%252Fphotos

更新3:

行。问题实际上是虚拟主机配置。文档根目录设置为C:\ wamp \ www \ myproject而不是C:\ wamp \ www \ myproject \ public。

我正在使用此htaccess重定向到公共文件夹:

RewriteEngine On

php_value upload_max_filesize 15M
php_value post_max_size 15M
php_value max_execution_time 200
php_value max_input_time 200
# Exclude some directories from URI rewriting
#RewriteRule ^(dir1|dir2|dir3) - [L]

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ /public/index.php [NC,L]

该死的我不知道为什么我忘了这个,我以为虚拟主机已经正确配置到公共文件夹,我100%肯定,我也仔细检查了它,看到没有问题(wtf?am我失明了?)。

1 个答案:

答案 0 :(得分:1)

  1. 检查你的php.ini
    php_flag magic_quotes_gpc off在线和本地服务器上必须Off

  2. 请勿使用urlencode 'ret' => urlencode('admin/index/photos'))因为url助手有第四个参数$encode = true,默认情况下为true

  3. 向我们展示服务器和localhost的$this->url(array('module' => 'admin'.....输出

  4. 我在家里使用wamp,它就像魅力一样

    更新1
    试试$this->_redirect( '/'. ltrim(urldecode($request->getParam('ret')), '/') );

    更新2;

    ViewScript

        <a href="<?php
    
    echo $this->url(array('module' => 'default',
                      'controller' => 'index',
                      'action' => 'test-encoded-redirect',
                      'ret' => urlencode('default/static/faq')),
                null,
                true);
    
                            ?>">test me</a>
    
    
        <?php
        class IndexController extends Smapp_Controller_Action 
        {   
    
            public function testEncodedRedirectAction()
            {
    
                                // ddump($this->_getAllParams());
                // ddump output 
                // Array ( 
                //  [controller] => index 
                //  [action] => test-encoded-redirect 
                //  [ret] => default%2Fstatic%2Ffaq 
                //  [module] => default 
                // )
    
    
                $ret = '/' . ltrim(urldecode($this->_getParam('ret')), '/');
                // echo $ret;
                // output: /default/static/faq
    
                $this->_redirect($ret);
            }
        }
    

    它有效!