gajus/director
Composer 安装命令:
composer require gajus/director
包简介
Utility for generating URLs relative to the predefined routes and for handling the redirects.
关键字:
README 文档
README
Utility for generating URLs relative to the predefined routes and for handling the redirects.
Use case
Use an instance of Router to generate URLs. It is convenient when your URL schema varies between the deployment environments.
URLs
Router instance carries predefined routes that are used to construct URLs.
/** * @param string $url Default route URL. */ $locator = new \Gajus\Director\Locator('http://gajus.com/'); /** * @todo Check if query string is included. * @param string $route_name Route name. * @param string $url Absolute URL. * @return null */ $locator->setRoute('static', 'http://static.gajus.com/'); # null /** * Get absolute URL using either of the predefined routes. * Requested resource path is appended to the route. * * @param string $path Relavite path to the route. * @param string $route Route name. */ $locator->url(); # http://gajus.com/ // Get URL relative to the default route: $locator->url('post/1'); # http://gajus.com/post/1 // Get URL for the "static" route: $locator->url(null, 'static'); # http://static.gajus.com/ // Get URL relative to the "static" route: $locator->url('css/frontend.css', 'static'); # http://static.gajus.com/css/frontend.css
Redirect
/** * Redirect user agent to the given URL. * * If no $url is provided, then attempt to redirect to the referrer * or (when referrer is not available) to the default route. * * @see http://benramsey.com/blog/2008/07/http-status-redirection/ * @param string|null $url Absolute URL * @param int|null $response_code HTTP response code. Defaults to 303 when request method is POST, 302 otherwise. * @return null */ $locator->location(); # null (script execution terminated) // Redirect to the default path with status code 307: $locator->location(null, 307); # null (script execution terminated) // Redirect to an arbitrary URL: $locator->location('http://gajus.com'); # null (script execution terminated)
location will throw Exception\LogicException exception if headers have been already sent.
Get path
The iverse of the url method is getPath. It is used to get the resource path of the current request URI relative to a specific route:
// Taken from ./tests/RouterTest.php $locator = new \Gajus\Director\Locator('https://gajus.com/foo/'); $_SERVER['HTTPS'] = 'on'; $_SERVER['HTTP_HOST'] = 'gajus.com'; $_SERVER['REQUEST_URI'] = '/foo/'; $this->assertSame('', $locator->getPath()); $_SERVER['HTTPS'] = 'on'; $_SERVER['HTTP_HOST'] = 'gajus.com'; $_SERVER['REQUEST_URI'] = '/foo/bar/'; $this->assertSame('bar/', $locator->getPath()); $_SERVER['HTTPS'] = 'on'; $_SERVER['HTTP_HOST'] = 'gajus.com'; $_SERVER['REQUEST_URI'] = '/foo/bar/?foo[bar]=1'; $this->assertSame('bar/', $locator->getPath());
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-04-21