生成 URL 和 路径Generating URLs and Paths

Phalcon\Mvc\Url 是phalcon应用中生成urls的组件。可以根据路由生成独立的urls.

Phalcon\Mvc\Url is the component responsible of generate urls in a Phalcon application. It’s capable of produce independent urls based on routes.

设置站点基地址 Setting a base URI

根据应用安装的根目录,会有个基uri或没有。

Depending of which directory of your document root your application is installed, it may have a base uri or not.

例如,如果文档根目录为/var/www/htdocs,应用安装在/var/www/htdocs/invo 。那基url就是/invo/。如果使用虚拟目录或者是安装在根目录则基url为/。执行如下代码去检查Phalcon的基url。

For example, if your document root is /var/www/htdocs and your application is installed in /var/www/htdocs/invo then your baseUri will be /invo/. If you are using a VirtualHost or your application is installed on the document root, then your baseUri is /. Execute the following code to know the base uri detected by Phalcon:

  1. <?php
  2. use Phalcon\Mvc\Url;
  3. $url = new Url();
  4. echo $url->getBaseUri();

默认情况下Phalcon自动检测到基url。为了增加应用搞的性能可以手动设置:

By default, Phalcon automatically may detect your baseUri, but if you want to increase the performance of your application is recommended setting up it manually:

  1. <?php
  2. use Phalcon\Mvc\Url;
  3. $url = new Url();
  4. //Setting a relative base URI
  5. $url->setBaseUri('/invo/');
  6. //Setting a full domain as base URI
  7. $url->setBaseUri('//my.domain.com/');
  8. //Setting a full domain as base URI
  9. $url->setBaseUri('http://my.domain.com/my-app/');

通常,这个组件在依赖注入容器中完成注册。如下所示:

Usually, this component must be registered in the Dependency Injector container, so you can set up it there:

  1. <?php
  2. use Phalcon\Mvc\Url;
  3. $di->set('url', function(){
  4. $url = new Url();
  5. $url->setBaseUri('/invo/');
  6. return $url;
  7. });

生成 URIGenerating URIs

如果使用 Router 的默认行为,我们的应用将匹配/:controller/:action/:params模式。使用get方法将非常简单的实现这种模式的链接:

If you are using the Router with its default behavior. Your application is able to match routes based on the following pattern: /:controller/:action/:params. Accordingly it is easy to create routes that satisfy that pattern (or any other pattern defined in the router) passing a string to the method “get”:

  1. <?php echo $url->get("products/save") ?>

注意并不需要必须添加基url的前缀。如果有一个命名的路由,可以动态的去变更,如下所示:

Note that isn’t necessary to prepend the base uri. If you have named routes you can easily change it creating it dynamically. For Example if you have the following route:

  1. <?php
  2. $route->add('/blog/{year}/{month}/{title}', array(
  3. 'controller' => 'posts',
  4. 'action' => 'show'
  5. ))->setName('show-post');

可以用如下方式生成url:

A URL can be generated in the following way:

  1. <?php
  2. //This produces: /blog/2012/01/some-blog-post
  3. $url->get(array(
  4. 'for' => 'show-post',
  5. 'year' => 2012,
  6. 'month' => '01',
  7. 'title' => 'some-blog-post'
  8. ));

非伪静态生成URL Producing URLs without Mod-Rewrite

使用这个组件还可以生成没有重写的url地址:

You can use this component also to create urls without mod-rewrite:

  1. <?php
  2. use Phalcon\Mvc\Url;
  3. $url = new Url();
  4. //Pass the URI in $_GET["_url"]
  5. $url->setBaseUri('/invo/index.php?_url=/');
  6. //This produce: /invo/index.php?_url=/products/save
  7. echo $url->get("products/save");

同样可以使用$_SERVER[“REQUEST_URI”]:

You can also use $_SERVER[“REQUEST_URI”]:

  1. <?php
  2. use Phalcon\Mvc\Url;
  3. $url = new Url();
  4. //Pass the URI in $_GET["_url"]
  5. $url->setBaseUri('/invo/index.php?_url=/');
  6. //Pass the URI using $_SERVER["REQUEST_URI"]
  7. $url->setBaseUri('/invo/index.php/');

这样需要在路由中手动处理请求的URI:

In this case, it’s necessary to manually handle the required URI in the Router:

  1. <?php
  2. use Phalcon\Mvc\Router;
  3. $router = new Router();
  4. // ... define routes
  5. $uri = str_replace($_SERVER["SCRIPT_NAME"], '', $_SERVER["REQUEST_URI"]);
  6. $router->handle($uri);

生成路由如下所示:

The produced routes would look like:

  1. <?php
  2. //This produce: /invo/index.php/products/save
  3. echo $url->get("products/save");

Volt 中生成 URL Producing URLs from Volt

在模板引擎volt中url是可以用来生成url的:

The function “url” is available in volt to generate URLs using this component:

  1. <a href="{{ url("posts/edit/1002") }}">Edit</a>

生成静态url:

Generate static routes:

  1. <link rel="stylesheet" href="{{ static_url("css/style.css") }}" type="text/css" />

静态 URI 与 动态 URI Static vs. Dynamic Uris

这个组件可以让我们在应用中为静态资源设置一个不同的基url地址:

This component allow you to set up a different base uri for static resources in the application:

  1. <?php
  2. use Phalcon\Mvc\Url;
  3. $url = new Url();
  4. //Dynamic URIs are
  5. $url->setBaseUri('/');
  6. //Static resources go through a CDN
  7. $url->setStaticBaseUri('http://static.mywebsite.com/');

使用 Phalcon\Tag 需要提供动态和静态的urls。

Phalcon\Tag will request both dynamical and static URIs using this component.

自定义 URL 生成器 Implementing your own Url Generator

如果要自定义phalcon中url生成方法 Phalcon\Mvc\UrlInterface 这个必须要被集成实现。

The Phalcon\Mvc\UrlInterface interface must be implemented to create your own URL generator replacing the one provided by Phalcon.