Skip to content

Commit

Permalink
uri function restored, router available in a global scope by default
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Mar 24, 2017
1 parent ac57cde commit 8d881ad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
6 changes: 5 additions & 1 deletion source/Spiral/Core/Bootloaders/SpiralBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Psr\Http\Message\ServerRequestInterface;
use Spiral\Core\Exceptions\ScopeException;
use Spiral\Http\HttpDispatcher;
use Spiral\Http\Routing\RouteInterface;

/**
Expand Down Expand Up @@ -69,7 +70,6 @@ class SpiralBindings extends Bootloader

//Http scope dependent
'cookies' => 'Spiral\Http\Cookies\CookieQueue',
'router' => 'Spiral\Http\Routing\RouterInterface',
'session' => 'Spiral\Session\SessionInterface',

//Pagination manager
Expand All @@ -92,6 +92,10 @@ class SpiralBindings extends Bootloader
//Scope depended
'actor' => 'Spiral\Security\ActorInterface',

//Default router is http specific
'Spiral\Http\Routing\RouterInterface' => [HttpDispatcher::class, 'getRouter'],
'router' => 'Spiral\Http\Routing\RouterInterface',

//Thought request attributes
'Spiral\Http\Routing\RouteInterface' => [self::class, 'activeRoute'],
];
Expand Down
22 changes: 22 additions & 0 deletions source/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
use Psr\Http\Message\UriInterface;
use Spiral\Core\Core;
use Spiral\Core\DirectoriesInterface;
use Spiral\Core\EnvironmentInterface;
use Spiral\Debug\Dumper;
use Spiral\Http\Routing\RouterInterface;
use Spiral\Translator\Exceptions\TranslatorException;
use Spiral\Translator\TranslatorInterface;

Expand Down Expand Up @@ -135,6 +137,26 @@ function interpolate(
}
}

if (!function_exists('uri')) {
/**
* Generate valid route URL using route name and set of parameters. Should support controller
* and action name separated by ":" - in this case router should find appropriate route and
* create url using it.
*
* @param string $route Route name.
* @param array|\Traversable $parameters
*
* @return UriInterface
* @throws \Spiral\Http\Exceptions\RouterException
* @throws \Spiral\Http\Exceptions\RouteException
* @throws \Spiral\Http\Exceptions\UndefinedRouteException
*/
function uri(string $route, $parameters = []): UriInterface
{
return spiral(RouterInterface::class)->uri($route, $parameters);
}
}

if (!function_exists('l')) {
/**
* Translate message using default or specific bundle name.
Expand Down
2 changes: 1 addition & 1 deletion tests/-app-/.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DEBUG = true
CACHE_BOOTLOADERS = false

#Encryption key used by Encrypter component to encrypt/decrypt data and protect your cookies
SPIRAL_KEY = ZGVmMDAwMDAyYTAzNzZiMjhlMzU2ZDdkYWIzYzNjNWQxYmMyMmZiYjg0ZWY4ODA1ZGQ2ZTFkMjVjOGU1YWM3YjYyNWFhYjk2MDlkZjdiYWIyOGFkYmNjM2I0ZDMyZTJkMGE3YWRjNmM1OWJlZWJiODk1NmNhNzE3YTgwOGM0OTUyMTAxMTEzYw==
SPIRAL_KEY = ZGVmMDAwMDBjN2E5YTFiOGFkMDQ5YTgzYzk4ZmE3ZmRmYmFhZTZhOWM5MzczZDE2ZWJlZDExYjZkNmZhMGIwOTg4MmQ4NmEzNDY3NjhiN2YwYjAyMDcwZjZiOTM1YTZiM2FjMDkwNDAwNDljZDkwMGZhNTA4NWJlZWU2MzQ4M2I5ZDk1NjNlZQ==

#Production applications must always have view cache turned on, disabled cache can only be useful
#in development
Expand Down
8 changes: 8 additions & 0 deletions tests/Http/UriGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@ public function testStrictUriWithQuery()

$this->assertSame('/pattern?a=1', (string)$router->uri('name', ['a' => 1]));
}

public function testStrictUriWithQueryViaUri()
{
$router = $this->http->getRouter();
$router->addRoute(new Route('name', '/pattern', 'target'));

$this->assertSame('/pattern?a=1', (string)uri('name', ['a' => 1]));
}
}

0 comments on commit 8d881ad

Please sign in to comment.