Skip to content

Commit

Permalink
[9.x] Fire event before route matched (#41765)
Browse files Browse the repository at this point in the history
* Fire event before route matched

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
timroberson and taylorotwell committed Apr 1, 2022
1 parent d80471d commit 8dd8918
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/Routing/Events/Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Routing\Events;

class Routing
{
/**
* The request instance.
*
* @var \Illuminate\Http\Request
*/
public $request;

/**
* Create a new event instance.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
public function __construct($request)
{
$this->request = $request;
}
}
3 changes: 3 additions & 0 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Events\Routing;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -674,6 +675,8 @@ public function dispatchToRoute(Request $request)
*/
protected function findRoute($request)
{
$this->events->dispatch(new Routing($request));

$this->current = $route = $this->routes->match($request);

$route->setContainer($this->container);
Expand Down
27 changes: 27 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Routing\Events\Routing;
use Illuminate\Routing\Exceptions\UrlGenerationException;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\ResourceRegistrar;
Expand Down Expand Up @@ -1523,6 +1524,32 @@ public function testRouterFiresRoutedEvent()
unset($_SERVER['__router.route']);
}

public function testRouterFiresRouteMatchingEvent()
{
$container = new Container;
$router = new Router($events = new Dispatcher, $container);
$container->singleton(Registrar::class, function () use ($router) {
return $router;
});
$router->get('foo/bar', function () {
return '';
});

$request = Request::create('http://foo.com/foo/bar', 'GET');

$_SERVER['__router.request'] = null;

$events->listen(Routing::class, function ($event) {
$_SERVER['__router.request'] = $event->request;
});

$router->dispatchToRoute($request);

$this->assertInstanceOf(Request::class, $_SERVER['__router.request']);
$this->assertEquals($_SERVER['__router.request'], $request);
unset($_SERVER['__router.request']);
}

public function testRouterPatternSetting()
{
$router = $this->getRouter();
Expand Down

0 comments on commit 8dd8918

Please sign in to comment.