webiny / router
Requires
- php: ^7
- webiny/cache: ~1.6
- webiny/config: ~1.6
- webiny/http: ~1.6
- webiny/service-manager: ~1.6
- webiny/std-lib: ~1.6
Requires (Dev)
- mybuilder/phpunit-accelerator: dev-master
- phpunit/phpunit: ~6
README
路由组件用于将定义的路径/URL映射到相应的控制器或服务。
安装组件
安装组件的最佳方式是使用 Composer。
composer require webiny/router
有关包的附加版本,请访问Packagist 页面。
用法
定义路由是一个相对简单的过程,你设置一个路由名称,然后在下面定义路径和回调。以下是一个示例
Router: Routes: BlogTag: Path: blog/tag/{tag} Callback: MyApp\Blog\showTag BlogComments: Path: blog/post/{id}/comments Callback: Class: MyApp\Blog Method: showComments BlogPost: Path: blog/post/{slug}/{id} Callback: MyApp\Blog\showPost Options: slug: Pattern: .*? BlogAuthor: Path: blog/authors/{author} Callback: MyApp\Blog\showAuthorPosts Options: author: Default: webiny Blog: Path: blog Callback: MyApp\Blog\index
如果匹配到路由,你将获得一个 MatchedRoute
实例。 getCallback
方法返回匹配路由的回调参数的值。第二个方法 getParams
返回在 Path
部分中定义的参数的值。
通过注册默认的 Router
配置,Router
将自动设置定义的路由和缓存驱动程序。
Router::setConfig($pathToYourConfig);
稍后你可以使用 RouterTrait
来访问那个 Router
实例。
注意: 当在配置文件中定义路由时,确保将精度较低的路由放在底部,将更严格的规则的路由放在顶部。一个常见的错误是,由于 Router
按照与配置文件中定义的相同顺序遍历定义的路由,并且一旦匹配到第一个路由就停止,所以具有更宽匹配模式的路由会先于具有更严格规则的路由匹配。
选项
在路由下提供的 Options
属性为您提供了两个额外的选项,您可以在每个路由下设置这些选项。
模式
匹配变量的默认正则表达式模式是 {[\w-]+}
。使用 Pattern
属性,您可以设置自己的规则。
默认值
Default
属性为您提供了设置变量默认值的选择。
如果使用当前可用的属性从 URL 路径匹配不到路由,Router
将将所有变量模式替换为默认值,并再次尝试匹配路由。
匹配路由
要检查路由是否与给定的路径匹配,请使用 RouterTrait
,然后只需调用 $this->router()->match()
方法。该方法接受一个表示 URL 的字符串,或者可以接受一个 Url
标准对象的实例。如果路由成功匹配其中一个路由,它将返回一个包含回调和参数属性的数组。如果路由没有匹配到任何路由,它将返回 false。
class MyClass { use \Webiny\Component\Router\RouterTrait; function __construct(){ $result = $this->router()->match('blog/tag/some_tag'); $result = $this->router()->match('http://www.webiny.com/blog/post/some-post/12'); } }
注意: Router
组件始终返回匹配给定路径的 第一个路由。
执行路由回调
如果您的回调定义为字符串,您将需要自行解析和执行它。但如果您遵循回调的标准结构,您将能够使用路由的 execute
方法,并将您的 MatchedRoute
传递给它。路由将为您执行回调,并执行有关类和方法存在性的所有检查。
BlogComments: Path: blog/post/{id}/comments Callback: Class: MyApp\Blog Method: showComments
class MyClass { use \Webiny\Component\Router\RouterTrait; function __construct(){ $result = $this->router()->match('blog/post/12/comments'); if($result){ $callbackResult = $this->router()->execute($result); } } }
如果出于某种原因需要静态调用方法,请像这样定义您的路由回调
BlogComments: Path: blog/post/{id}/comments Callback: Class: MyApp\Blog Method: showComments Static: true
生成路由
使用 Router
,您不需要在代码或视图中编写 URL,而是可以从给定的路由生成 URL,如下所示
class MyClass { use \Webiny\Component\Router\RouterTrait; function __construct(){ $url = $this->router()->generate('BlogTag', ['tag'=>'html5', 'page'=>1]); // http://www.webiny.com/blog/tag/html5/?page=1 // page was not defined in the route, that's why it's appended as a query param $url = $this->router()->generate('BlogAuthor'); // http://www.webiny.com/blog/authors/webiny // the value of "author" was not set, so the generator used the default value } }
您可以看到,Router
将提供的值 {tag}
参数进行了替换,在这种情况下是 html5
。您还可以注意到,在路由中未定义 page
参数,因此 Router
将该参数追加为查询字符串。
配置
Router
组件还接受一个额外的配置参数,即 Cache
参数。您可以为将要用于缓存一些内部机制的缓存服务命名,或者如果您不希望组件使用缓存,则可以将其设置为 false
。
Router: Cache: TestCache
使用缓存可以加快路由的匹配速度,尤其是更复杂的路由。
资源
要运行单元测试,需要使用以下命令
$ cd path/to/Webiny/Component/Router/
$ composer.phar install
$ phpunit