Skip to content

Commit

Permalink
set route parameters (#24)
Browse files Browse the repository at this point in the history
* set route parameters

* Fix code styling

---------

Co-authored-by: taylorotwell <taylorotwell@users.noreply.github.com>
  • Loading branch information
taylorotwell and taylorotwell authored Jul 10, 2023
1 parent faf5c61 commit 1d007cf
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/FolioManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function handler(MountPath $mountPath): Closure
public function middlewareFor(string $uri): array
{
foreach ($this->mountPaths as $mountPath) {
if (! $matchedView = (new Router($mountPath->path))->match($uri)) {
if (! $matchedView = (new Router($mountPath->path))->match(new Request, $uri)) {
continue;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Pipeline/TransformModelBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
namespace Laravel\Folio\Pipeline;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class TransformModelBindings
{
/**
* Create a new pipeline step instance.
*/
public function __construct(protected Request $request)
{
}

/**
* Invoke the routing pipeline handler.
*/
Expand Down Expand Up @@ -57,6 +65,12 @@ public function __invoke(State $state, Closure $next): mixed
$parent = $resolved;
}

if ($this->request->route()) {
foreach ($view->data as $key => $value) {
$this->request->route()->setParameter($key, $value);
}
}

return $view;
}

Expand Down
2 changes: 1 addition & 1 deletion src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __invoke(Request $request, string $uri): mixed
{
$matchedView = (new Router(
$this->mountPath->path
))->match($uri) ?? abort(404);
))->match($request, $uri) ?? abort(404);

return (new Pipeline(app()))
->send($request)
Expand Down
9 changes: 5 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Folio;

use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Arr;
use Laravel\Folio\Pipeline\ContinueIterating;
Expand Down Expand Up @@ -37,12 +38,12 @@ public function __construct(array|string $mountPaths)
/**
* Match the given URI to a view via page based routing.
*/
public function match(string $uri): ?MatchedView
public function match(Request $request, string $uri): ?MatchedView
{
$uri = strlen($uri) > 1 ? trim($uri, '/') : $uri;

foreach ($this->mountPaths as $mountPath) {
if ($view = $this->matchAtPath($mountPath, $uri)) {
if ($view = $this->matchAtPath($mountPath, $request, $uri)) {
return $view;
}
}
Expand All @@ -53,7 +54,7 @@ public function match(string $uri): ?MatchedView
/**
* Resolve the given URI via page based routing at the given mount path.
*/
protected function matchAtPath(string $mountPath, string $uri): ?MatchedView
protected function matchAtPath(string $mountPath, Request $request, string $uri): ?MatchedView
{
$state = new State(
uri: $uri,
Expand All @@ -66,7 +67,7 @@ protected function matchAtPath(string $mountPath, string $uri): ?MatchedView
->send($state->forIteration($i))
->through([
new EnsureNoDirectoryTraversal,
new TransformModelBindings,
new TransformModelBindings($request),
new SetMountPathOnMatchedView,
// ...
new MatchRootIndex,
Expand Down
31 changes: 16 additions & 15 deletions tests/Feature/RoutingTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Laravel\Folio\Exceptions\PossibleDirectoryTraversal;

$purgeDirectories = function () {
Expand All @@ -19,8 +20,8 @@

$router = $this->router();

expect(realpath(__DIR__.'/../tmp/views/index.blade.php'))->toEqual($router->match('/')->path)
->and($router->match('/missing-view'))->toBeNull();
expect(realpath(__DIR__.'/../tmp/views/index.blade.php'))->toEqual($router->match(new Request, '/')->path)
->and($router->match(new Request, '/missing-view'))->toBeNull();
});

test('directory index views can be matched', function () {
Expand All @@ -32,7 +33,7 @@

$router = $this->router();

expect(realpath(__DIR__.'/../tmp/views/users/index.blade.php'))->toEqual($router->match('/users')->path);
expect(realpath(__DIR__.'/../tmp/views/users/index.blade.php'))->toEqual($router->match(new Request, '/users')->path);
});

test('literal views can be matched', function () {
Expand All @@ -43,7 +44,7 @@

$router = $this->router();

expect(realpath(__DIR__.'/../tmp/views/profile.blade.php'))->toEqual($router->match('/profile')->path);
expect(realpath(__DIR__.'/../tmp/views/profile.blade.php'))->toEqual($router->match(new Request, '/profile')->path);
});

test('wildcard views can be matched', function () {
Expand All @@ -54,7 +55,7 @@

$router = $this->router();

$resolved = $router->match('/1');
$resolved = $router->match(new Request, '/1');

expect(realpath(__DIR__.'/../tmp/views/[id].blade.php'))->toEqual($resolved->path)
->and($resolved->data)->toEqual(['id' => 1]);
Expand All @@ -69,11 +70,11 @@

$router = $this->router();

$resolved = $router->match('/profile');
$resolved = $router->match(new Request, '/profile');

expect(realpath(__DIR__.'/../tmp/views/profile.blade.php'))->toEqual($resolved->path)
->and($resolved->data)->toEqual([])
->and($router->match('/profile/missing-view'))->toBeNull();
->and($router->match(new Request, '/profile/missing-view'))->toBeNull();
});

test('literal views may be in directories', function () {
Expand All @@ -85,7 +86,7 @@

$router = $this->router();

expect(realpath(__DIR__.'/../tmp/views/users/profile.blade.php'))->toEqual($router->match('/users/profile')->path);
expect(realpath(__DIR__.'/../tmp/views/users/profile.blade.php'))->toEqual($router->match(new Request, '/users/profile')->path);
});

test('wildcard views may be in directories', function () {
Expand All @@ -97,7 +98,7 @@

$router = $this->router();

$resolved = $router->match('/users/1');
$resolved = $router->match(new Request, '/users/1');

expect(realpath(__DIR__.'/../tmp/views/users/[id].blade.php'))->toEqual($resolved->path);

Expand All @@ -113,7 +114,7 @@

$router = $this->router();

expect($router->match('/users/1'))->toBeNull();
expect($router->match(new Request, '/users/1'))->toBeNull();
});

test('multisegment wildcard views can be matched', function () {
Expand All @@ -123,7 +124,7 @@

$router = $this->router();

$resolved = $router->match('/1/2/3');
$resolved = $router->match(new Request, '/1/2/3');

expect(realpath(__DIR__.'/../tmp/views/[...id].blade.php'))->toEqual($resolved->path);

Expand All @@ -140,7 +141,7 @@

$router = $this->router();

$resolved = $router->match('/1/2/3');
$resolved = $router->match(new Request, '/1/2/3');

expect(realpath(__DIR__.'/../tmp/views/[...id].blade.php'))->toEqual($resolved->path);

Expand All @@ -158,7 +159,7 @@

$router = $this->router();

$resolved = $router->match('/flights/1/connections');
$resolved = $router->match(new Request, '/flights/1/connections');

expect(realpath(__DIR__.'/../tmp/views/flights/[id]/connections.blade.php'))->toEqual($resolved->path);

Expand All @@ -180,7 +181,7 @@

$router = $this->router();

$resolved = $router->match('/flights/1/connections/2/map');
$resolved = $router->match(new Request, '/flights/1/connections/2/map');

expect(realpath(__DIR__.'/../tmp/views/flights/[id]/connections/[connectionId]/map.blade.php'))->toEqual($resolved->path)
->and($resolved->data)->toEqual(['id' => 1, 'connectionId' => 2]);
Expand All @@ -189,5 +190,5 @@
it('ensures directory traversal is not possible', function () {
$router = $this->router();

$router->match('/../');
$router->match(new Request, '/../');
})->throws(PossibleDirectoryTraversal::class);
27 changes: 14 additions & 13 deletions tests/Unit/ModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

$purgeDirectories = function () {
Expand All @@ -24,7 +25,7 @@

$router = $this->router();

$view = $router->match('/users/1');
$view = $router->match(new Request, '/users/1');

$this->assertTrue(
$view->data['folioModelBindingTestClass'] instanceof FolioModelBindingTestClass
Expand All @@ -43,7 +44,7 @@

$router = $this->router();

$router->match('/users/_missing');
$router->match(new Request, '/users/_missing');
})->throws(ModelNotFoundException::class);

test('implicit model bindings with more than one binding in path', function () {
Expand All @@ -60,7 +61,7 @@

$router = $this->router();

$view = $router->match('/users/1/posts/2');
$view = $router->match(new Request, '/users/1/posts/2');

$this->assertTrue(
$view->data['first'] instanceof FolioModelBindingTestClass
Expand All @@ -87,7 +88,7 @@

$router = $this->router();

$view = $router->match('/users/1');
$view = $router->match(new Request, '/users/1');

$this->assertEquals(
'slug',
Expand All @@ -107,7 +108,7 @@

$router = $this->router();

$view = $router->match('/users/1');
$view = $router->match(new Request, '/users/1');

$this->assertEquals(
'1',
Expand All @@ -130,7 +131,7 @@

$router = $this->router();

$view = $router->match('/users/1');
$view = $router->match(new Request, '/users/1');

$this->assertEquals(
$field,
Expand All @@ -157,7 +158,7 @@

$router = $this->router();

$view = $router->match('/users/abc');
$view = $router->match(new Request, '/users/abc');

$this->assertEquals(
'ABC',
Expand All @@ -175,7 +176,7 @@

$router = $this->router();

$view = $router->match('/users/1/2/3');
$view = $router->match(new Request, '/users/1/2/3');

$this->assertTrue(is_array($view->data['folioModelBindingTestClasses']));
$this->assertEquals('1', $view->data['folioModelBindingTestClasses'][0]->value);
Expand All @@ -195,7 +196,7 @@

$router = $this->router();

$view = $router->match('/users/1/2/3');
$view = $router->match(new Request, '/users/1/2/3');

$this->assertTrue(is_array($view->data[$variable]));

Expand Down Expand Up @@ -225,7 +226,7 @@

$router = $this->router();

$view = $router->match('/users/1/posts/2');
$view = $router->match(new Request, '/users/1/posts/2');

$this->assertEquals('1', $view->data['first']->value);

Expand Down Expand Up @@ -259,7 +260,7 @@

$router = $this->router();

$view = $router->match('/users/abc/posts/def');
$view = $router->match(new Request, '/users/abc/posts/def');

$this->assertEquals('ABC', $view->data['first']->value);
$this->assertEquals('DEF', $view->data['second']->value);
Expand All @@ -281,7 +282,7 @@

$router = $this->router();

$router->match('/users/1/posts/_missing');
$router->match(new Request, '/users/1/posts/_missing');
})->throws(ModelNotFoundException::class);

test('model bindings can be enums', function () {
Expand All @@ -293,7 +294,7 @@

$router = $this->router();

$view = $router->match('/categories/posts');
$view = $router->match(new Request, '/categories/posts');

$this->assertEquals('posts', $view->data['category']->value);

Expand Down

0 comments on commit 1d007cf

Please sign in to comment.