# 路由(Routing)
The router component allows you to define routes that are mapped to controllers or handlers that should receive the request. A router simply parses a URI to determine this information. The router has two modes: MVC mode and match-only mode. The first mode is ideal for working with MVC applications.
路由器組件用來定義處理接收到的請求的路由,指向相應的控制器或者處理程序。路由器只是簡單解析一個URI獲取這些信息。 路由器有兩種模式:MVC模式以及匹配模式。第一種模式主要適合MVC應用。
## 定義路由(Defining Routes)
[Phalcon\\Mvc\\Router](http://docs.iphalcon.cn/api/Phalcon_Mvc_Router.html)provides advanced routing capabilities. In MVC mode, you can define routes and map them to controllers/actions that you require. A route is defined as follows:
[Phalcon\\Mvc\\Router](http://docs.iphalcon.cn/api/Phalcon_Mvc_Router.html)提供高級路由支持。在MVC模式下,你可以定義路由并映射向需要的控制器/動作。 一個路由定義方法如下所示:
~~~
<?php
use Phalcon\Mvc\Router;
// Create the router
$router = new Router();
// Define a route
$router->add(
"/admin/users/my-profile",
[
"controller" => "users",
"action" => "profile",
]
);
// Another route
$router->add(
"/admin/users/change-password",
[
"controller" => "users",
"action" => "changePassword",
]
);
$router->handle();
~~~
add() 方法接受一個匹配模式作為第一個參數,一組可選的路徑作為第二個參數。如上,如果URI就是/admin/users/my-profile的話, 那么 “users” 控制的 “profile” 方法將被調用。當然路由器并不馬上就調用這個方法,它只是收集這些信息并且通知相應的組件( 比如[Phalcon\\Mvc\\Dispatcher](http://docs.iphalcon.cn/api/Phalcon_Mvc_Dispatcher.html))應該調用這個控制器的這個動作。
一個應用程序可以由很多路徑,一個一個定義是一個非常笨重的工作。這種情況下我們可以創建一個更加靈活的路由:
~~~
<?php
use Phalcon\Mvc\Router;
// Create the router
$router = new Router();
// Define a route
$router->add(
"/admin/:controller/a/:action/:params",
[
"controller" => 1,
"action" => 2,
"params" => 3,
]
);
~~~
在上面的例子中我們通過使用通配符定義了一個可以匹配多個URI的路由,比如,訪問這個URL(/admin/users/a/delete/dave/301),那么:
ControllerusersActiondeleteParameterdaveParameter301
The`add()`method receives a pattern that can optionally have predefined placeholders and regular expression modifiers. All the routing patterns must start with a forward slash character (/). The regular expression syntax used is the same as the[PCRE regular expressions](http://www.php.net/manual/en/book.pcre.php). Note that, it is not necessary to add regular expression delimiters. All route patterns are case-insensitive.
The second parameter defines how the matched parts should bind to the controller/action/parameters. Matching parts are placeholders or subpatterns delimited by parentheses (round brackets). In the example given above, the first subpattern matched (`:controller`) is the controller part of the route, the second the action and so on.
These placeholders help writing regular expressions that are more readable for developers and easier to understand. The following placeholders are supported:
| Placeholder | Regular Expression | Usage |
| --- | --- | --- |
| `/:module` | `/([a-zA-Z0-9\_\-]+)` | Matches a valid module name with alpha-numeric characters only |
| `/:controller` | `/([a-zA-Z0-9\_\-]+)` | Matches a valid controller name with alpha-numeric characters only |
| `/:action` | `/([a-zA-Z0-9\_]+)` | Matches a valid action name with alpha-numeric characters only |
| `/:params` | `(/.*)*` | Matches a list of optional words separated by slashes. Only use this placeholder at the end of a route |
| `/:namespace` | `/([a-zA-Z0-9\_\-]+)` | Matches a single level namespace name |
| `/:int` | `/([0-9]+)` | Matches an integer parameter |
Controller names are camelized, this means that characters (`-`) and (`_`) are removed and the next character is uppercased. For instance, some\_controller is converted to SomeController.
Since you can add many routes as you need using the`add()`method, the order in which routes are added indicate their relevance, latest routes added have more relevance than first added. Internally, all defined routes are traversed in reverse order until[Phalcon\\Mvc\\Router](http://docs.iphalcon.cn/api/Phalcon_Mvc_Router.html)finds the one that matches the given URI and processes it, while ignoring the rest.
### 參數名稱(Parameters with Names)
下面的例子演示了如何定義路由參數:
~~~
<?php
$router->add(
"/news/([0-9]{4})/([0-9]{2})/([0-9]{2})/:params",
[
"controller" => "posts",
"action" => "show",
"year" => 1, // ([0-9]{4})
"month" => 2, // ([0-9]{2})
"day" => 3, // ([0-9]{2})
"params" => 4, // :params
]
);
~~~
在上述示例中,路由規則里并沒有定義 “controller” 或者 “action” 部分。它們已經被路由替換為(“posts” and “show”)。 用戶不會知道請求當中實際分發到的是哪個控制器。在控制器內部,可以通過如下方式來接收參數:
~~~
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
}
public function showAction()
{
// Get "year" parameter
$year = $this->dispatcher->getParam("year");
// Get "month" parameter
$month = $this->dispatcher->getParam("month");
// Get "day" parameter
$day = $this->dispatcher->getParam("day");
// ...
}
}
~~~
Note that the values of the parameters are obtained from the dispatcher. This happens because it is the component that finally interacts with the drivers of your application. Moreover, there is also another way to create named parameters as part of the pattern:
~~~
<?php
$router->add(
"/documentation/{chapter}/{name}.{type:[a-z]+}",
[
"controller" => "documentation",
"action" => "show",
]
);
~~~
You can access their values in the same way as before:
~~~
<?php
use Phalcon\Mvc\Controller;
class DocumentationController extends Controller
{
public function showAction()
{
// Get "name" parameter
$name = $this->dispatcher->getParam("name");
// Get "type" parameter
$type = $this->dispatcher->getParam("type");
// ...
}
}
~~~
### 短語法(Short Syntax)
If you don’t like using an array to define the route paths, an alternative syntax is also available. The following examples produce the same result:
~~~
<?php
// Short form
$router->add(
"/posts/{year:[0-9]+}/{title:[a-z\-]+}",
"Posts::show"
);
// Array form
$router->add(
"/posts/([0-9]+)/([a-z\-]+)",
[
"controller" => "posts",
"action" => "show",
"year" => 1,
"title" => 2,
]
);
~~~
### 混合使用數組和短語法(Mixing Array and Short Syntax)
Array and short syntax can be mixed to define a route, in this case note that named parameters automatically are added to the route paths according to the position on which they were defined:
~~~
<?php
// First position must be skipped because it is used for
// the named parameter 'country'
$router->add(
"/news/{country:[a-z]{2}}/([a-z+])/([a-z\-+])",
[
"section" => 2, // Positions start with 2
"article" => 3,
]
);
~~~
### 路由到模塊(Routing to Modules)
你可以在路由規則中包含模塊。這種用法特別適合于多模塊的應用程序。It’s possible define a default route that includes a module wildcard:
~~~
<?php
use Phalcon\Mvc\Router;
$router = new Router(false);
$router->add(
"/:module/:controller/:action/:params",
[
"module" => 1,
"controller" => 2,
"action" => 3,
"params" => 4,
]
);
~~~
在上述示例中,URL中必須總是含有模塊名才能進行路由解析。比如URL: /admin/users/edit/sonny, 將會被路由解析為:
ModuleadminControllerusersActioneditParametersonny
你也可以將特定的路由規則綁定到特定的模塊:
~~~
<?php
$router->add(
"/login",
[
"module" => "backend",
"controller" => "login",
"action" => "index",
]
);
$router->add(
"/products/:action",
[
"module" => "frontend",
"controller" => "products",
"action" => 1,
]
);
~~~
Or bind them to specific namespaces:
~~~
<?php
$router->add(
"/:namespace/login",
[
"namespace" => 1,
"controller" => "login",
"action" => "index",
]
);
~~~
Namespaces/class names must be passed separated:
~~~
<?php
$router->add(
"/login",
[
"namespace" => "Backend\\Controllers",
"controller" => "login",
"action" => "index",
]
);
~~~
### 限制 HTTP 請求傳入方式(HTTP Method Restrictions)
當使用`add()`方法來添加路由規則時, 這條路由規則可以支持HTTP協議的任何數據傳輸方法。 有時我們需要限制路由規則只能匹配HTTP協議的某個方法,這在創建 RESTful 風格的應用程序時特別有用:
~~~
<?php
// This route only will be matched if the HTTP method is GET
$router->addGet(
"/products/edit/{id}",
"Products::edit"
);
// This route only will be matched if the HTTP method is POST
$router->addPost(
"/products/save",
"Products::save"
);
// This route will be matched if the HTTP method is POST or PUT
$router->add(
"/products/update",
"Products::update"
)->via(
[
"POST",
"PUT",
]
);
~~~
### 使用轉換(Using conversors)
Conversors allow you to freely transform the route’s parameters before passing them to the dispatcher. The following examples show how to use them:
~~~
<?php
// The action name allows dashes, an action can be: /products/new-ipod-nano-4-generation
$route = $router->add(
"/products/{slug:[a-z\-]+}",
[
"controller" => "products",
"action" => "show",
]
);
$route->convert(
"slug",
function ($slug) {
// Transform the slug removing the dashes
return str_replace("-", "", $slug);
}
);
~~~
Another use case for conversors is binding a model into a route. This allows the model to be passed into the defined action directly:
~~~
<?php
// This example works off the assumption that the ID is being used as parameter in the url: /products/4
$route = $router->add(
"/products/{id}",
[
"controller" => "products",
"action" => "show",
]
);
$route->convert(
"id",
function ($id) {
// Fetch the model
return Product::findFirstById($id);
}
);
~~~
### 路由分組(Groups of Routes)
If a set of routes have common paths they can be grouped to easily maintain them:
~~~
<?php
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Router\Group as RouterGroup;
$router = new Router();
// Create a group with a common module and controller
$blog = new RouterGroup(
[
"module" => "blog",
"controller" => "index",
]
);
// All the routes start with /blog
$blog->setPrefix("/blog");
// Add a route to the group
$blog->add(
"/save",
[
"action" => "save",
]
);
// Add another route to the group
$blog->add(
"/edit/{id}",
[
"action" => "edit",
]
);
// This route maps to a controller different than the default
$blog->add(
"/blog",
[
"controller" => "blog",
"action" => "index",
]
);
// Add the group to the router
$router->mount($blog);
~~~
You can move groups of routes to separate files in order to improve the organization and code reusing in the application:
~~~
<?php
use Phalcon\Mvc\Router\Group as RouterGroup;
class BlogRoutes extends RouterGroup
{
public function initialize()
{
// Default paths
$this->setPaths(
[
"module" => "blog",
"namespace" => "Blog\\Controllers",
]
);
// All the routes start with /blog
$this->setPrefix("/blog");
// Add a route to the group
$this->add(
"/save",
[
"action" => "save",
]
);
// Add another route to the group
$this->add(
"/edit/{id}",
[
"action" => "edit",
]
);
// This route maps to a controller different than the default
$this->add(
"/blog",
[
"controller" => "blog",
"action" => "index",
]
);
}
}
~~~
Then mount the group in the router:
~~~
<?php
// Add the group to the router
$router->mount(
new BlogRoutes()
);
~~~
## 匹配路由(Matching Routes)
A valid URI must be passed to the Router so that it can process it and find a matching route. By default, the routing URI is taken from the`$_GET["_url"]`variable that is created by the rewrite engine module. A couple of rewrite rules that work very well with Phalcon are:
~~~
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
~~~
In this configuration, any requests to files or folders that don’t exist will be sent to index.php.
The following example shows how to use this component in stand-alone mode:
~~~
<?php
use Phalcon\Mvc\Router;
// Creating a router
$router = new Router();
// Define routes here if any
// ...
// Taking URI from $_GET["_url"]
$router->handle();
// Or Setting the URI value directly
$router->handle("/employees/edit/17");
// Getting the processed controller
echo $router->getControllerName();
// Getting the processed action
echo $router->getActionName();
// Get the matched route
$route = $router->getMatchedRoute();
~~~
## 路由命名(Naming Routes)
Each route that is added to the router is stored internally as a[Phalcon\\Mvc\\Router\\Route](http://docs.iphalcon.cn/api/Phalcon_Mvc_Router_Route.html)object. That class encapsulates all the details of each route. For instance, we can give a name to a path to identify it uniquely in our application. This is especially useful if you want to create URLs from it.
~~~
<?php
$route = $router->add(
"/posts/{year}/{title}",
"Posts::show"
);
$route->setName("show-posts");
~~~
Then, using for example the component[Phalcon\\Mvc\\Url](http://docs.iphalcon.cn/api/Phalcon_Mvc_Url.html)we can build routes from its name:
~~~
<?php
// Returns /posts/2012/phalcon-1-0-released
echo $url->get(
[
"for" => "show-posts",
"year" => "2012",
"title" => "phalcon-1-0-released",
]
);
~~~
## 范例(Usage Examples)
The following are examples of custom routes:
~~~
<?php
// Matches "/system/admin/a/edit/7001"
$router->add(
"/system/:controller/a/:action/:params",
[
"controller" => 1,
"action" => 2,
"params" => 3,
]
);
// Matches "/es/news"
$router->add(
"/([a-z]{2})/:controller",
[
"controller" => 2,
"action" => "index",
"language" => 1,
]
);
// Matches "/es/news"
$router->add(
"/{language:[a-z]{2}}/:controller",
[
"controller" => 2,
"action" => "index",
]
);
// Matches "/admin/posts/edit/100"
$router->add(
"/admin/:controller/:action/:int",
[
"controller" => 1,
"action" => 2,
"id" => 3,
]
);
// Matches "/posts/2015/02/some-cool-content"
$router->add(
"/posts/([0-9]{4})/([0-9]{2})/([a-z\-]+)",
[
"controller" => "posts",
"action" => "show",
"year" => 1,
"month" => 2,
"title" => 4,
]
);
// Matches "/manual/en/translate.adapter.html"
$router->add(
"/manual/([a-z]{2})/([a-z\.]+)\.html",
[
"controller" => "manual",
"action" => "show",
"language" => 1,
"file" => 2,
]
);
// Matches /feed/fr/le-robots-hot-news.atom
$router->add(
"/feed/{lang:[a-z]+}/{blog:[a-z\-]+}\.{type:[a-z\-]+}",
"Feed::get"
);
// Matches /api/v1/users/peter.json
$router->add(
"/api/(v1|v2)/{method:[a-z]+}/{param:[a-z]+}\.(json|xml)",
[
"controller" => "api",
"version" => 1,
"format" => 4,
]
);
~~~
> Beware of characters allowed in regular expression for controllers and namespaces. As these become class names and in turn they’re passed through the file system could be used by attackers to read unauthorized files. A safe regular expression is:`/([a-zA-Z0-9\_\-]+)`
## 默認行為(Default Behavior)
[Phalcon\\Mvc\\Router](http://docs.iphalcon.cn/api/Phalcon_Mvc_Router.html)has a default behavior that provides a very simple routing that always expects a URI that matches the following pattern: /:controller/:action/:params
For example, for a URL like this*http://phalconphp.com/documentation/show/about.html*, this router will translate it as follows:
ControllerdocumentationActionshowParameterabout.html
If you don’t want the router to have this behavior, you must create the router passing`false`as the first parameter:
~~~
<?php
use Phalcon\Mvc\Router;
// Create the router without default routes
$router = new Router(false);
~~~
## 設置默認路由(Setting the default route)
When your application is accessed without any route, the ‘/’ route is used to determine what paths must be used to show the initial page in your website/application:
~~~
<?php
$router->add(
"/",
[
"controller" => "index",
"action" => "index",
]
);
~~~
## 沒有找到路徑(Not Found Paths)
If none of the routes specified in the router are matched, you can define a group of paths to be used in this scenario:
~~~
<?php
// Set 404 paths
$router->notFound(
[
"controller" => "index",
"action" => "route404",
]
);
~~~
This is typically for an Error 404 page.
## 設置默認路徑(Setting default paths)
It’s possible to define default values for the module, controller or action. When a route is missing any of those paths they can be automatically filled by the router:
可以為通用路徑中的 module, controller, action 定義默認值。當一個路由缺少其中任何一項時,路由器可以自動用默認值填充:
~~~
<?php
// Setting a specific default
$router->setDefaultModule("backend");
$router->setDefaultNamespace("Backend\\Controllers");
$router->setDefaultController("index");
$router->setDefaultAction("index");
// Using an array
$router->setDefaults(
[
"controller" => "index",
"action" => "index",
]
);
~~~
## 處理結尾額外的斜桿(Dealing with extra/trailing slashes)
Sometimes a route could be accessed with extra/trailing slashes. Those extra slashes would lead to produce a not-found status in the dispatcher. You can set up the router to automatically remove the slashes from the end of handled route:
~~~
<?php
use Phalcon\Mvc\Router;
$router = new Router();
// Remove trailing slashes automatically
$router->removeExtraSlashes(true);
~~~
Or, you can modify specific routes to optionally accept trailing slashes:
~~~
<?php
// The [/]{0,1} allows this route to have optionally have a trailing slash
$router->add(
"/{language:[a-z]{2}}/:controller[/]{0,1}",
[
"controller" => 2,
"action" => "index",
]
);
~~~
## 匹配回調函數(Match Callbacks)
Sometimes, routes should only be matched if they meet specific conditions. You can add arbitrary conditions to routes using the`beforeMatch()`callback. If this function return`false`, the route will be treated as non-matched:
~~~
<?php
$route = $router->add("/login",
[
"module" => "admin",
"controller" => "session",
]
);
$route->beforeMatch(
function ($uri, $route) {
// Check if the request was made with Ajax
if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && $_SERVER["HTTP_X_REQUESTED_WITH"] === "XMLHttpRequest") {
return false;
}
return true;
}
);
~~~
You can re-use these extra conditions in classes:
~~~
<?php
class AjaxFilter
{
public function check()
{
return $_SERVER["HTTP_X_REQUESTED_WITH"] === "XMLHttpRequest";
}
}
~~~
And use this class instead of the anonymous function:
~~~
<?php
$route = $router->add(
"/get/info/{id}",
[
"controller" => "products",
"action" => "info",
]
);
$route->beforeMatch(
[
new AjaxFilter(),
"check"
]
);
~~~
As of Phalcon 3, there is another way to check this:
~~~
<?php
$route = $router->add(
"/login",
[
"module" => "admin",
"controller" => "session",
]
);
$route->beforeMatch(
function ($uri, $route) {
/**
* @var string $uri
* @var \Phalcon\Mvc\Router\Route $route
* @var \Phalcon\DiInterface $this
* @var \Phalcon\Http\Request $request
*/
$request = $this->getShared("request");
// Check if the request was made with Ajax
return $request->isAjax();
}
);
~~~
## 限制主機名(Hostname Constraints)
The router allows you to set hostname constraints, this means that specific routes or a group of routes can be restricted to only match if the route also meets the hostname constraint:
~~~
<?php
$route = $router->add(
"/login",
[
"module" => "admin",
"controller" => "session",
"action" => "login",
]
);
$route->setHostName("admin.company.com");
~~~
The hostname can also be passed as a regular expressions:
~~~
<?php
$route = $router->add(
"/login",
[
"module" => "admin",
"controller" => "session",
"action" => "login",
]
);
$route->setHostName("([a-z]+).company.com");
~~~
In groups of routes you can set up a hostname constraint that apply for every route in the group:
~~~
<?php
use Phalcon\Mvc\Router\Group as RouterGroup;
// Create a group with a common module and controller
$blog = new RouterGroup(
[
"module" => "blog",
"controller" => "posts",
]
);
// Hostname restriction
$blog->setHostName("blog.mycompany.com");
// All the routes start with /blog
$blog->setPrefix("/blog");
// Default route
$blog->add(
"/",
[
"action" => "index",
]
);
// Add a route to the group
$blog->add(
"/save",
[
"action" => "save",
]
);
// Add another route to the group
$blog->add(
"/edit/{id}",
[
"action" => "edit",
]
);
// Add the group to the router
$router->mount($blog);
~~~
## URI 來源(URI Sources)
By default the URI information is obtained from the`$_GET["_url"]`variable, this is passed by the Rewrite-Engine to Phalcon, you can also use`$_SERVER["REQUEST_URI"]`if required:
~~~
<?php
use Phalcon\Mvc\Router;
// ...
// Use $_GET["_url"] (default)
$router->setUriSource(
Router::URI_SOURCE_GET_URL
);
// Use $_SERVER["REQUEST_URI"]
$router->setUriSource(
Router::URI_SOURCE_SERVER_REQUEST_URI
);
~~~
Or you can manually pass a URI to the`handle()`method:
~~~
<?php
$router->handle("/some/route/to/handle");
~~~
## 測試路由(Testing your routes)
Since this component has no dependencies, you can create a file as shown below to test your routes:
~~~
<?php
use Phalcon\Mvc\Router;
// These routes simulate real URIs
$testRoutes = [
"/",
"/index",
"/index/index",
"/index/test",
"/products",
"/products/index/",
"/products/show/101",
];
$router = new Router();
// Add here your custom routes
// ...
// Testing each route
foreach ($testRoutes as $testRoute) {
// Handle the route
$router->handle($testRoute);
echo "Testing ", $testRoute, "<br>";
// Check if some route was matched
if ($router->wasMatched()) {
echo "Controller: ", $router->getControllerName(), "<br>";
echo "Action: ", $router->getActionName(), "<br>";
} else {
echo "The route wasn't matched by any route<br>";
}
echo "<br>";
}
~~~
## 注解路由(Annotations Router)
這個組件利用集成的注解服務[annotations](http://docs.iphalcon.cn/reference/annotations.html)提供了一個路由定義的變體。通過這個策略,你可以直接在書寫控制器 的時候編寫路由,而不需要一個一個在服務注冊的時候添加。
~~~
<?php
use Phalcon\Mvc\Router\Annotations as RouterAnnotations;
$di["router"] = function () {
// Use the annotations router. We're passing false as we don't want the router to add its default patterns
$router = new RouterAnnotations(false);
// Read the annotations from ProductsController if the URI starts with /api/products
$router->addResource("Products", "/api/products");
return $router;
};
~~~
注解通過如下的方式定義:
~~~
<?php
/**
* @RoutePrefix("/api/products")
*/
class ProductsController
{
/**
* @Get(
* "/"
* )
*/
public function indexAction()
{
}
/**
* @Get(
* "/edit/{id:[0-9]+}",
* name="edit-robot"
* )
*/
public function editAction($id)
{
}
/**
* @Route(
* "/save",
* methods={"POST", "PUT"},
* name="save-robot"
* )
*/
public function saveAction()
{
}
/**
* @Route(
* "/delete/{id:[0-9]+}",
* methods="DELETE",
* conversors={
* id="MyConversors::checkId"
* }
* )
*/
public function deleteAction($id)
{
}
public function infoAction($id)
{
}
}
~~~
只有標記了格式正確的注解的方法才能被用作路由。Phalcon支持如下注解:
| 名稱 | 描述 | 用法 |
| --- | --- | --- |
| RoutePrefix | A prefix to be prepended to each route URI. This annotation must be placed at the class’ docblock | `@RoutePrefix("/api/products")` |
| Route | This annotation marks a method as a route. This annotation must be placed in a method docblock | `@Route("/api/products/show")` |
| Get | This annotation marks a method as a route restricting the HTTP method to GET | `@Get("/api/products/search")` |
| Post | This annotation marks a method as a route restricting the HTTP method to POST | `@Post("/api/products/save")` |
| Put | This annotation marks a method as a route restricting the HTTP method to PUT | `@Put("/api/products/save")` |
| Delete | This annotation marks a method as a route restricting the HTTP method to DELETE | `@Delete("/api/products/delete/{id}")` |
| Options | This annotation marks a method as a route restricting the HTTP method to OPTIONS | `@Option("/api/products/info")` |
用來添加路由的注解支持如下參數:
| 名稱 | 描述 | 用法 |
| --- | --- | --- |
| methods | Define one or more HTTP method that route must meet with | `@Route("/api/products",methods={"GET","POST"})` |
| name | Define a name for the route | `@Route("/api/products",name="get-products")` |
| paths | An array of paths like the one passed to`Phalcon\Mvc\Router::add()` | `@Route("/posts/{id}/{slug}",paths={module="backend"})` |
| conversors | A hash of conversors to be applied to the parameters | `@Route("/posts/{id}/{slug}",conversors={id="MyConversor::getId"})` |
如果路由對應的控制器屬于一個模塊,使用`addModuleResource()`效果更佳:
~~~
<?php
use Phalcon\Mvc\Router\Annotations as RouterAnnotations;
$di["router"] = function () {
// Use the annotations router
$router = new RouterAnnotations(false);
// Read the annotations from Backend\Controllers\ProductsController if the URI starts with /api/products
$router->addModuleResource("backend", "Products", "/api/products");
return $router;
};
~~~
## 注冊路由實例(Registering Router instance)
You can register router during service registration with Phalcon dependency injector to make it available inside the controllers.
You need to add code below in your bootstrap file (for example index.php or app/config/services.php if you use[Phalcon Developer Tools](http://phalconphp.com/en/download/tools))
~~~
<?php
/**
* Add routing capabilities
*/
$di->set(
"router",
function () {
require __DIR__ . "/../app/config/routes.php";
return $router;
}
);
~~~
You need to create app/config/routes.php and add router initialization code, for example:
~~~
<?php
use Phalcon\Mvc\Router;
$router = new Router();
$router->add(
"/login",
[
"controller" => "login",
"action" => "index",
]
);
$router->add(
"/products/:action",
[
"controller" => "products",
"action" => 1,
]
);
return $router;
~~~
## 自定義路由(Implementing your own Router)
The[Phalcon\\Mvc\\RouterInterface](http://docs.iphalcon.cn/api/Phalcon_Mvc_RouterInterface.html)interface must be implemented to create your own router replacing the one provided by Phalcon.
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl