Zend Framework:为其他网站创建网址

时间:2014-07-28 08:58:15

标签: url zend-framework frameworks

我是Zend的新手,我不知道如何从文本中生成url,例如。 'example.com'应为'http://example.com/'。如何简单地做到这一点?

1 个答案:

答案 0 :(得分:1)

urlserverUrl个助手。见initial helpers。或者你可以write a custom helper

serverUrl helper

serverUrl($requestUri = null)

用于返回当前服务器URL的助手(可选地带有请求URI)。

// Current server URL in the example is: http://www.example.com/foo.html

echo $this->serverUrl();
// Output: http://www.example.com

echo $this->serverUrl(true);
// Output: http://www.example.com/foo.html

echo $this->serverUrl('/foo/bar');
// Output: http://www.example.com/foo/bar

echo $this->serverUrl()->getHost();
// Output: www.example.com

echo $this->serverUrl()->getScheme();
// Output: http

$this->serverUrl()->setHost('www.foo.com');
$this->serverUrl()->setScheme('https');
echo $this->serverUrl();
// Output: https://www.foo.com

Url helper

url($urlOptions, $name, $reset, $encode)

根据命名路由创建URL字符串。 $ urlOptions应该是特定路由使用的键/值对的关联数组。

// Using without options: (current request is: user/id/1)
echo $this->url();
// Output: user/info/id/1

// Set URL options:
echo $this->url(
    array('controller' => 'user', 'action' => 'info', 'username' => 'foobar')
);
// Output: user/info/username/foobar

// Using a route:
$router->addRoute(
    'user',
    new Zend_Controller_Router_Route(
        'user/:username',
        array(
            'controller' => 'user',
            'action'     => 'info',
        )
    )
);

echo $this->url(array('name' => 'foobar'), 'user');
// Output: user/foobar

// Using reset: (current request is: user/id/1)
echo $this->url(array('controller' => 'user', 'action' => 'info'), null, false);
// Output: user/info/id/1

echo $this->url(array('controller' => 'user', 'action' => 'info'), null, true);
// Output: user/info

// Using encode:
echo $this->url(
    array('controller' => 'user', 'action' => 'info', 'username' => 'John Doe'), null, true, false
);
// Output: user/info/username/John Doe

echo $this->url(
    array('controller' => 'user', 'action' => 'info', 'username' => 'John Doe'), null, true, false
);
// Output: user/info/username/John+Doe