URL 生成

Testing Is Documentation

tests/Router/UrlTest.phpURL 生成 - 图1

QueryPHP 支持路由 URL 地址的统一生成,提供一套简洁的生成方法,无需记忆即可学会使用。

使用容器 url 服务

  1. \App::make('url')->make(string $url, array $attributes = [], string $subdomain = 'www', $suffix = null): string;

Uses

  1. <?php
  2. use Leevel\Http\Request;
  3. use Leevel\Router\Url;

基本 URL 生成

  1. public function testMakeUrl(): void
  2. {
  3. $request = $this->makeRequest();
  4. $url = new Url($request);
  5. $this->assertInstanceof(Request::class, $url->getRequest());
  6. // 开始不带斜线,自动添加
  7. $this->assertSame($url->make('test/hello'), '/test/hello');
  8. $this->assertSame($url->make('/hello-world'), '/hello-world');
  9. }

生成带参数的 URL

  1. public function testMakeUrlWithParams(): void
  2. {
  3. $request = $this->makeRequest();
  4. $url = new Url($request);
  5. $this->assertSame($url->make('test/hello?arg1=1&arg2=3'), '/test/hello?arg1=1&arg2=3');
  6. $this->assertSame($url->make('test/sub1/sub2/hello?arg1=1&arg2=3'), '/test/sub1/sub2/hello?arg1=1&arg2=3');
  7. $this->assertSame($url->make('test/sub1/sub2/hello', ['arg1' => 1, 'arg2' => 3]), '/test/sub1/sub2/hello?arg1=1&arg2=3');
  8. }

生成带后缀的 URL

  1. public function testMakeUrlWithSuffix(): void
  2. {
  3. $request = $this->makeRequest();
  4. $url = new Url($request);
  5. $this->assertSame($url->make('hello/world', [], '', true), '/hello/world.html');
  6. $this->assertSame($url->make('hello/world', [], '', '.jsp'), '/hello/world.jsp');
  7. }

生成 URL 支持变量替换

  1. public function testMakeUrlSupportVar(): void
  2. {
  3. $request = $this->makeRequest();
  4. $url = new Url($request);
  5. $this->assertSame($url->make('test/{id}?arg1=5', ['id' => 5]), '/test/5?arg1=5');
  6. $this->assertSame($url->make('/new-{id}-{name}', ['id' => 5, 'name' => 'tom', 'arg1' => '5']), '/new-5-tom?arg1=5');
  7. $this->assertSame($url->make('/new-{id}-{name}?hello=world', ['id' => 5, 'name' => 'tom', 'arg1' => '5']), '/new-5-tom?hello=world&arg1=5');
  8. $this->assertSame($url->make('/new-{id}-{name}?hello={foo}', ['id' => 5, 'name' => 'tom', 'foo' => 'bar', 'arg1' => '5']), '/new-5-tom?hello=bar&arg1=5');
  9. }

生成指定应用的 URL

  1. public function testMakeUrlForApp(): void
  2. {
  3. $request = $this->makeRequest();
  4. $url = new Url($request);
  5. $this->assertSame($url->make(':myapp/hello/world', ['id' => 5, 'name' => 'yes']), '/:myapp/hello/world?id=5&name=yes');
  6. $this->assertSame($url->make(':myapp/test'), '/:myapp/test');
  7. }

生成首页地址

  1. public function testMakeUrlForHome(): void
  2. {
  3. $request = $this->makeRequest();
  4. $url = new Url($request);
  5. $this->assertSame($url->make('/'), '/');
  6. $this->assertSame($url->make(''), '/');
  7. }

生成带域名的 URL

  1. public function testWithDomainTop(): void
  2. {
  3. $request = $this->makeRequest();
  4. $url = new Url($request, [
  5. 'domain' => 'queryphp.com',
  6. ]);
  7. $this->assertSame($url->make('hello/world'), 'http://www.queryphp.com/hello/world');
  8. $this->assertSame($url->make('hello/world', [], 'vip'), 'http://vip.queryphp.com/hello/world');
  9. $this->assertSame($url->make('hello/world', [], 'defu.vip'), 'http://defu.vip.queryphp.com/hello/world');
  10. $this->assertSame($url->make('hello/world', [], '*'), 'http://queryphp.com/hello/world');
  11. }

生成带 HTTPS 的 URL

  1. public function testSecureWithDomainTop(): void
  2. {
  3. $request = $this->makeRequest(true);
  4. $url = new Url($request, [
  5. 'domain' => 'queryphp.cn',
  6. ]);
  7. $this->assertInstanceof(Request::class, $url->getRequest());
  8. $this->assertSame($url->make('hello/world'), 'https://www.queryphp.cn/hello/world');
  9. }